Next: , Previous: , Up: Structures   [Contents][Index]


15.17 Unnamed Types as Fields

A structure or a union can contain, as fields, unnamed structures and unions. Here’s an example:

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

You can access the fields of the unnamed union within foo as if they were individual fields at the same level as the union definition:

foo.a = 42;
foo.b = 47;
foo.c = 5.25; // Overwrites the value in foo.b.
foo.d = 314;

Avoid using field names that could cause ambiguity. For example, with this definition:

struct
{
  int a;
  struct
  {
    int a;
    float b;
  };
} foo;

it is impossible to tell what foo.a refers to. GNU C reports an error when a definition is ambiguous in this way.