eggert@twinsun.com (Paul Eggert) (10/20/89)
GCC should process initializers with elided braces independently of whether
they initialize unions or structures. For example, the following program
should initialize x and y to identical values:
union u {
int a[2];
};
union u x = { { 1, 2 } };
union u y = { 1, 2 };
Unfortunately, GCC complains about the second initialization above on a
Sparcstation-1 under SunOS 4.0.3c:
% gcc -v t.c
gcc version 1.36
/local/lib/gcc-cpp -v -undef -D__GNUC__ -Dsparc -Dsun -Dunix -D__sparc__ -D__sun__ -D__unix__ t.c /usr/tmp/cca04523.cpp
GNU CPP version 1.36
/local/lib/gcc-cc1 /usr/tmp/cca04523.cpp -quiet -dumpbase t.c -version -o /usr/tmp/cca04523.s
GNU C version 1.36 (sparc) compiled by GNU C version 1.36.
default target switches: -mfpu -mepilogue
t.c:6: union initializer requires one element
GCC does not complain if 'union' is uniformly changed to 'struct'.
Here's why GCC's behavior should change. The proposed ANSI standard says
(section 3.5.7, page 73, lines 21-27 of the December 7, 1988 draft):
If the aggregate contains members that are aggregates or unions, or if
the first member of a union is an aggregate or union,... [and] if the
initializer of a subaggregate or contained union [does not begin]...
with a left brace,... only enough initializers from the list are taken
to account for the members of the subaggregate or the first member of
the contained union;...
That is, with respect to eliding braces, the standard uses the same language
for initializing unions as it does for initializing other aggregates including
structures. In particular, braces can be elided for a union with just one
member in the same way that they can be elided for a structure with just one
member.