Initialising an union.

Jeroen Dobbelaere Jeroen.Dobbelaere@tvd.be
Tue Aug 18 11:46:00 GMT 1998


Hi,

The initialisation of a union only initializes the first member
of the union, not the 'largest' member. (the remaining part can be
uninitialized.


(from the december 1997 draft working paper :)

  8.5  Initializers                                           [dcl.init]

1 A declarator can specify an initial value  for  the  identifier  being

[...]

5 To zero-initialize storage for an object of type T means:
[..]

  --if  T  is  a  union type, the storage for its first data member5) is
    zero-initialized;

[.....]

15When  a  union  is  initialized with a brace-enclosed initializer, the
  braces shall only contain an initializer for the first member  of  the
  union.  [Example:
          union u { int a; char* b; };

          u a = { 1 };
          u b = a;
          u c = 1;              // error
          u d = { 0, "asdf" };  // error
          u e = { "asdf" };     // error
    --end example] [Note: as described above, the braces around the ini-
  tializer for a union member can be omitted if the union is a member of
  another aggregate.  ]

Greetings,

Jeroen Dobbelaere



More information about the Gcc-bugs mailing list