Retired Documents Library Developer
Search

Next: , Previous: Pragmas, Up: C Extensions


5.54 Unnamed struct/union fields within structs/unions

For compatibility with other compilers, GCC allows you to define a structure or union that contains, as fields, structures and unions without names. For example:

     struct {
       int a;
       union {
         int b;
         float c;
       };
       int d;
     } foo;

In this example, the user would be able to access members of the unnamed union with code like `foo.b'. Note that only unnamed structs and unions are allowed, you may not have, for example, an unnamed int.

You must never create such structures that cause ambiguous field definitions. For example, this structure:

     struct {
       int a;
       struct {
         int a;
       };
     } foo;

It is ambiguous which a is being referred to with `foo.a'. Such constructs are not supported and must be avoided. In the future, such constructs may be detected and treated as compilation errors.

Unless -fms-extensions is used, the unnamed field must be a structure or union definition without a tag (for example, `struct { int a; };'). If -fms-extensions is used, the field may also 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 a typedef name for a previously defined structure or union type.