gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:37:27 25 4
gpt4 key购买 nike

代码

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com