在编译简单的链表程序时得到未声明节点的错误

时间:2015-09-13 09:04:55

标签: c data-structures linked-list structure

#include <stdio.h>

void main()
{
     struct Node      //defining a node
     {
          int data;             //defining the data member
          struct Node *next;    //defining a pointer to Node type variable
     };

      struct Node *head;        //declaring a pointer variable 'head' to a Node type variable.   
      head=NULL;                //Since the head pointer now points nothing,so initialised with NULL.

      struct Node* temp = (Node*)malloc(sizeof(struct Node));//creating a node and storing its adrs in pointer 'temp'
     (*temp).data=2;          //node's data part is initialised.
     (*temp).next= NULL;    //the node points to nothing initially
     head=temp;             //head is initialised with address of the node,so now it points to the node 

     printf("%d",temp->data);
 }

1 个答案:

答案 0 :(得分:0)

你应该写

struct Node* temp = (struct Node*)malloc(sizeof(struct Node));

而不是

 struct Node* temp = (Node*)malloc(sizeof(struct Node));

因为struct标签与通常的标识符位于不同的名称空间中。因此,必须使用struct关键字来指定.Read this

此外,包括<stdlib.h>,否则您将收到警告,表明您正在隐式声明malloc。