This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Initialising an union.


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]