指针在C自我结构 [英] Pointer to self struct in C
问题描述
由于以下code:
typedef struct elementT {
int data;
struct elementT *next;
} element;
为什么有必要做结构elementT *下一
,我不能做元素*下一
里面的结构声明本身?是不是因为它是尚未宣布?
Why is it necessary to do struct elementT *next
and I can't do element *next
inside the struct declaration itself? Is it because it is not declared yet?
推荐答案
的的typedef
只发生后,结构
已被定义。请看下面的code;语法是无效的,但它希望它显示订单/ $ P $的东西pcedence:
The typedef
only takes place after the struct
has been defined. Consider the following code; the syntax is invalid, but it hopes it shows the order/precedence of things:
typedef (struct {
int field1, field2;
}) item;
即,在结构{...}
是一个前pression,它表示一个类型。通过给它一个名称的该类型的typedef
工作
I.e., the struct{...}
is an "expression" that denotes a type. The typedef
operates on that type by giving it a name.
相反,
struct foo {
foo *next;
};
工程的C因为两个特殊的规则:结构<名称>
不仅定义了一个类型,但给它一个结构
标记为好,而标签是结构
声明的其余部分(原因很明显内立即可见:链表,树等,将是非常痛苦没有这些规则来实现)。
works because of two special rules of C: struct <name>
not only defines a type, but gives it a struct
tag as well, and that tag is immediately visible inside the rest of the struct
declaration (for the obvious reason: linked lists, trees, etc. would be very painful to implement without these rules).
这篇关于指针在C自我结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!