开发者

How do I get rid of this initialization from incompatible pointer type warning/error?

开发者 https://www.devze.com 2023-04-05 10:07 出处:网络
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?Wh

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消