c - 编译简单链表程序时出现未声明节点错误

标签 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);
 }

最佳答案

你应该写

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

而不是

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

因为结构标记与通常的标识符位于不同的命名空间中。因此需要使用 struct 关键字来指定这一点。阅读 this

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

关于c - 编译简单链表程序时出现未声明节点错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32548061/

相关文章:

C 宏/#define 缩进?

用 C 捕获当前桌面 View

c++ - 我怎样才能得到一个程序的状态?

python-3.x - 为什么我的蛮力(O((n1 + n2)log(n1 + n2)))解决方案比优化解决方案(O(n1 + n2))更快?

c - K+R 2.4 : bus error when assigning (Mac OS)

data-structures - 在 Haskell 代数数据类型中的备选方案中进行选择

algorithm - MINIMUM 函数第 4 行的平均运行时间是多少?

c - 双 while 循环链表导致无限循环

c++ - 如何将 find_if 与链表等非容器一起使用?

c++ - 为什么在 C++ 中的函数内创建对象是一种不好的做法?