http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53608
Bug #: 53608 Summary: Documentation could be clearer about designated initializers of unions Classification: Unclassified Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: web AssignedTo: unassig...@gcc.gnu.org ReportedBy: alan.coopersm...@oracle.com http://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html currently says: If the same field is initialized multiple times, it will have value from the last initialization. (Which should probably say "the value" instead.) Currently gcc applies this to multiple components of a union type as well. (At least with gcc 3.4 through 4.5 here.) For instance, this program: #include <stdio.h> int main(int argc, char **argv) { union { struct { int i; char c[4]; } a; struct { int i; short s[2]; } b; } u = { .a.i = 1, .b.s[0] = 2, .b.s[1] = 3 }; printf("%d %d %d\n", u.a.i, u.b.s[0], u.b.s[1]); } will print "0 2 3" when compiled with gcc, since gcc appears to treat this as two initializations of the "same field" (fields mapped to the same spot in memory): u.a = { 1 }; u.b = { 0, 2, 3}; and discards the first. (By comparison, the Solaris Studio compiler prints "1 2 3" for this code.) If the current behavior of gcc is considered correct, then it would be helpful to update the documentation to say something like: If the same field is initialized multiple times, or overlapping members of the same union are initialized, the value from the last initialization will be used. When union members are structures, the entire structure from the last member initialized is used. For example: union { struct { int i; char c[4]; } a; struct { int i; short s[2]; } b; } u = { .a.i = 1, .b.s[0] = 2, .b.s[1] = 3 }; is equivalent to: union { struct { int i; char c[4]; } a; struct { int i; short s[2]; } b; } u = { .a = { 1 }, .b = { 0, 2, 3 } }; which is in turn equivalent to: union { struct { int i; char c[4]; } a; struct { int i; short s[2]; } b; } u = { .b = { 0, 2, 3 } };