结构体定义中引用自身结构体指针怎么理解

2024-10-29 10:56:00
推荐回答(1个)
回答1:

指针只是个4位的数值,指的是内存地址,所以在构造的时候是指占4字节的(32位下),里面具体指谁是在用的时候才定的,给你个例子关于指针的
struct node {
int data;
struct node *next;
};

int main(void)
{
struct node *head, first, second;

head = &first;
first.data = 1;
first.next = &second;

second.data = 2;
second.next = NULL;

while (head) {
printf("%d\n", head->data);
head = head->next;
}
return 0;
}