我有下面的代码:
#include<stdio.h>
typedef struct node{
int value;
struct node *next;
}node;
int main(){
node *a;
return 0;
}
谁能向我解释为什么a必须是指针而不是变量类型节点?感谢您的帮助
最佳答案
链表的下一个节点由struct node *next;
给出
根据定义,这必须是一个指针。不能将 struct node
变量包含在 struct node
typedef struct node{
int value;
struct node next; //-- Not Allowed
}node;
上述声明将无法编译,因为结构节点
的大小将是无限的。
链表的头可以是非指针,但这不必要地使代码变得复杂,因为您需要分别为头和其他节点设置两个代码段。
关于c - 为什么链表的变量必须是指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59367884/