1)a是类型的变量struct hello。就像你可以定义
struct hello a;
如果struct hello已经知道。
您还可以内联甚至匿名定义它
struct {
// some members
} a;
2) 只是*a在这种情况下是无效的。任何编译器都会告诉您不能取消引用非指针类型。a这是一个类型struct hello。
另一方面struct hello *a;是有效的并定义a为指向struct hello.
3)我不知道保留关键字的任何标准库node。所以定义 astruct node应该是完全有效的。
如 2)struct node *x;没有定义临时存储。它定义了一个指向struct node被调用的指针x。它最初不会分配内存,但一旦分配内存,您就可以通过*x.
定义
struct node {
*x;
};
不是有效的 C。结构的成员必须是有效的 C 类型。否则,您的编译器不知道 stuct 占用了多少内存以及如何访问这些成员。
但是像
struct node {
char *x;
};
说得通。它用pinter 类型struct node的成员定义 a 到。xchar
4) 结构中的结构是完全有效的,因为您可以为结构成员提供任何有效的 C 类型。例子:
struct test {
struct hello a; // using an already defined struct
struct test * next; // using a pointer to own struct
// or anonymous structs
// normally used for readability of code
struct {
char member1;
int member2;
} anonymous_struct;
// or even unions are allowed here
// anonymous here
union {
int a;
double b;
} u;
} nested_struct;
这些现在可以作为嵌套结构访问:
nested_struct.a
nested_struct.next->a
nested_struct.anonymous_struct.member1
nested_struct.u.b
将struct rusage定义一个活的例子,sys/resource.h其中有一个嵌套struct timeval定义在sys/time.h. http://linux.die.net/man/2/getrusage
请注意,它x->a在语法上等同于(*x).a并且只能在x是指向结构的指针时使用。