Anonymous Union

In C++, an anonymous union is a union without a tag or declarators; it declares an unnamed object. An anonymous union cannot have member functions or private or protected members.

A global anonymous union must be static. A local anonymous union must be either static or automatic, not external.

In C, an anonymous union can have a tag; it cannot have declarators.

The name of each member must be unique within the scope where the union is declared. You can directly access the members of an anonymous union as shown in the example.

Example

static union       // Global anonymous unions must be static
{
   int i;
   float f;
   union
   {
      char c;
      unsigned char uc;
   };
};

void my_func()
{
   i = 1;
   f = 3.14;
   c = 'c';
   uc = 'u';
};