I am trying to write a linked list program in C, however I keep on getting an initalization from incompatible pointer type warning/error. How do I get rid of this? And could you explain what is wrong? What follow开发者_运维技巧s is a simplified version of my program:
typedef struct node
{
int contents;
struct Node *nextNode;
} Node;
int main(void)
{
//.......Other code here......
Node *rootNode = (Node *) malloc(sizeof(Node));
rootNode->nextNode = NULL;
//.......Other code here......
addNode(rootNode);
}
addNode(Node *currentNode)
{
//.....Other code here....
Node *nextNode = (currentNode->nextNode); //Error on this line
// ....Other code here...
}
Thanks
I think you want struct node *
not struct Node *
in your struct node
:
typedef struct node
{
int contents;
struct node *nextNode; /* here */
} Node;
And don't cast the return value from malloc
, it is not needed.
精彩评论