gcc-4.4.3 lets you statically initialize named fields, and lets you assign to anonymous union members, but you cannot statically initialize a named member of an anonymous union. This inconsistency between assignment and initialization is surprising, and breaks some code I am porting from an EDG-based compiler to gcc-4.4.3. Let me emphasize that I don't claim this violates any official standard, since these are language extensions. But it does violate the "principle of least surprise", since one would expect that the "assignment" and "initialization" field namespaces would be the same, as with EDG. Here is an example: // Union with anonymous member. union my_union { int my_int; struct { short a, b; }; }; // Works: gcc lets you initialize named fields. union my_union initialize_int() { union my_union x = { .my_int = 3 }; return x; } // Works: gcc lets you assign to anonymous union members. union my_union assign_anonymous_union(union my_union x) { x.a = 3; return x; } // Fails: gcc does not let you initialize named members in anonymous unions. union my_union initialize_anonymous_union() { // Does not compile, // // foo.c: In function ‘initialize_anonymous_union’: // foo.c:33: error: unknown field ‘a’ specified in initializer union my_union x = { .a = 3 }; return x; }
*** This bug has been marked as a duplicate of 10676 ***