Next:Cast to a Union Type, Previous:Structures with only Flexible Array Members, Up:Array, Union, and Struct Extensions [Contents][Index]
As permitted by ISO C11 and for compatibility with other compilers,GCC allows you to definea structure or union that contains, as fields, structures and unionswithout names. For example:
struct { int a; union { int b; float c; }; int d;} foo;In this example, you are able to access members of the unnamedunion with code like ‘foo.b’. Note that only unnamed structs andunions are allowed, you may not have, for example, an unnamedint.
You must never create such structures that cause ambiguous field definitions.For example, in this structure:
struct { int a; struct { int a; };} foo;it is ambiguous whicha is being referred to with ‘foo.a’.The compiler gives errors for such constructs.
Unless-fms-extensions is used, the unnamed field must be astructure or union definition without a tag (for example, ‘struct{ int a; };’). If-fms-extensions is used, the field mayalso be a definition with a tag such as ‘struct foo { int a;};’, a reference to a previously defined structure or union such as‘struct foo;’, or a reference to atypedef name for apreviously defined structure or union type.
The option-fplan9-extensions enables-fms-extensions as well as two other extensions. First, apointer to a structure is automatically converted to a pointer to ananonymous field for assignments and function calls. For example:
struct s1 { int a; };struct s2 { struct s1; };extern void f1 (struct s1 *);void f2 (struct s2 *p) { f1 (p); }In the call tof1 insidef2, the pointerp isconverted into a pointer to the anonymous field.
Second, when the type of an anonymous field is atypedef for astruct orunion, code may refer to the field using thename of thetypedef.
typedef struct { int a; } s1;struct s2 { s1; };s1 f1 (struct s2 *p) { return p->s1; }These usages are only permitted when they are not ambiguous.
Next:Cast to a Union Type, Previous:Structures with only Flexible Array Members, Up:Array, Union, and Struct Extensions [Contents][Index]