Error 'a value of type “X *” cannot be assigne

2019-02-25 07:04发布

问题:

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

回答1:

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

 typedef struct Node { ....


回答2:

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;
}


回答3:

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;
};