简体   繁体   中英

Error 'a value of type “X *” cannot be assigned to an entity of type “X *”' when using typedef struct

Here is the struct I am using for the nodes...

typedef struct
{
    struct Node* next;
    struct Node* previous;
    void* data;
} Node;

and here is the function I am using to link them

void linkNodes(Node* first, Node* second)
{
    if (first != NULL)
        first->next = second;

    if (second != NULL)
        second->previous = first;
}

now visual studio is giving me the intellisense(less) error on those lines

IntelliSense: a value of type "Node *" cannot be assigned to an entity of type "Node *"

can anyone explain the proper way to do this? Visual studio will compile it and run it find and it also works on my mac but is crashing on my schools servers.

edit: i thought of using memcpy but that's pretty cheasy

I think the problem is there is no struct called Node, there is only a typedef. Try

 typedef struct Node { ....

Similar to Deepu's answer, but a version that will let your code compile. Change your struct to the following:

typedef struct Node // <-- add "Node"
{
    struct Node* next;
    struct Node* previous;
    void* data;
}Node; // <-- Optional

void linkNodes(Node* first, Node* second)
{    
    if (first != NULL)
        first->next = second;

    if (second != NULL)
        second->previous = first;
}

Defining typedef of struct in C is best done before the struct declaration itself.

typedef struct Node Node; // forward declaration of struct and typedef

struct Node
{
    Node* next;          // here you only need to use the typedef, now
    Node* previous;
    void* data;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM