我有一个关于结构定义和指针的问题。
在链表节点结构的定义中,我们按照以下方式定义结构:
typedef struct node
{
int data;
struct node *next;
}Node;
为什么我们要使用这种声明方式而不是:
typedef struct node
{
int data;
struct node next; //changed this line
}Node;
提前感谢!
我有一个关于结构定义和指针的问题。
在链表节点结构的定义中,我们按照以下方式定义结构:
typedef struct node
{
int data;
struct node *next;
}Node;
为什么我们要使用这种声明方式而不是:
typedef struct node
{
int data;
struct node next; //changed this line
}Node;
提前感谢!
在它的闭合大括号之后,结构体被定义。在此之前,结构体只有不完整类型。但是,结构体的定义要求除了柔性数组以外的所有成员都必须具有完整类型。
因此,在这个声明中
typedef struct node
{
int data;
struct node next; //changed this line
}Node;
struct node *
类型,由架构确定了明确的大小。struct node next
,next的大小将是多少?它会是struct node
的大小吗?好的,假设它是,但再次问一下 - struct node
的大小是多少?嗯,答案是sizeof(int) + sizeof(struct node)
。好吧,但再次...等等...什么?...回去重新读这整个段落,意识到这里的进退两难局面...