union { int raw; struct { int foo; }; } baz = { .raw = 0 }, /* OK */ zab = { .foo = 1 }; /* unknown field `foo' specified in initializer */ Unnamed unions/structs are very useful. If the initializer for zab were valid, they would be even more useful. Thanks for your consideration, Marcus Release: gcc (GCC) 3.2.2 20030124 (Debian prerelease) Environment: Debian GNU/Linux i386-linux-gnu
Happens on the mainline (20030531), 3.3.1 (20030526), 3.2.3, and 3.0.4: tin:~/src/gnu/gcctest>gcc pr10676.c pr10676.c:9: error: unknown field `foo' specified in initializer tin:~/src/gnu/gcctest>~/ia32_linux_gcc3_2/bin/gcc pr10676.c pr10676.c:9: unknown field `foo' specified in initializer tin:~/src/gnu/gcctest>~/ia32_linux_gcc3_3/bin/gcc pr10676.c pr10676.c:9: error: unknown field `foo' specified in initializer tin:~/src/gnu/gcctest>~/ia32_linux_gcc3_0/bin/gcc pr10676.c pr10676.c:9: unknown field `foo' specified in initializer
*** Bug 14094 has been marked as a duplicate of this bug. ***
Still happens on 4.0 snapshot of 20050213 : $ /gnu/bin/gcc -c pr10676.c pr10676.c:9: error: unknown field 'foo' specified in initializer $ /gnu/bin/gcc -v ... gcc version 4.0.0 20050213 (experimental)
*** Bug 24065 has been marked as a duplicate of this bug. ***
*** Bug 30737 has been marked as a duplicate of this bug. ***
*** Bug 38019 has been marked as a duplicate of this bug. ***
Still happens with GCC 4.3.2: $ gcc -v Using built-in specs. Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-cld --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.3.2 (Debian 4.3.2-1)
How can I update the "last confirmed" date to today?
Similarly, I would like the following to work as an extension to the anonymous union support: Given a structure like: struct foo { int a; union { int b; float c; }; int d; }; I want to initialize it with a statement like: struct foo value_of_foo = { .a = 0, .b = 1, .d = 2 }; instead of what is currently required, with braces around the initialization of 'foo::b': struct foo value_of_foo = { .a = 0, { .b = 1 }, .d = 2 }; This is a natural extension of the ability to access members 'b' and 'c' without tagging their union type.
*** Bug 40656 has been marked as a duplicate of this bug. ***
An example program that shows 3 different methods. Only one works (see comment) As I used the names for each field, I assumed that all 3 examples should work. #include <inttypes.h> #include <stdio.h> struct a_t { uint16_t a; uint16_t b; union { uint8_t c; uint16_t d; }; }; #if (1) struct a_t alfa = { .a = 1, .b = 2, .c = 3, //Will not work }; #elif (0) struct a_t alfa = { .a = 1, .b = 2, {.c = 3}, //works }; #elif (0) struct a_t alfa = { .a = 1, {.c= 2}, //Does not work .b = 3, } #endif int main(void) { //alfa.c = 4; //alfa.d = 5; printf("%20s:%d\n","alfa.a",alfa.a); printf("%20s:%d\n","alfa.b",alfa.b); printf("%20s:%d\n","alfa.c",alfa.c); printf("%20s:%d\n","alfa.d",alfa.d); return 0; }
*** Bug 42875 has been marked as a duplicate of this bug. ***
This "bug" is causing me difficulty porting from an EDG-based compiler to gcc-4.4.3. EDG implemented "assignment" and "initialization" as having the same namespace.
C1x anonymous structures and unions will likely require a fix for this.
Subject: Bug 10676 Author: jsm28 Date: Sun May 9 20:39:39 2010 New Revision: 159206 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=159206 Log: PR c/10676 * c-typeck.c (lookup_field): Take a type directly. Update recursive calls. (build_component_ref): Update call to lookup_field. (set_init_label): Use lookup_field to find initialized field. Handle returned list of fields like a sequence of designators. testsuite: * gcc.dg/anon-struct-10.c: New test. Added: trunk/gcc/testsuite/gcc.dg/anon-struct-10.c Modified: trunk/gcc/ChangeLog trunk/gcc/c-typeck.c trunk/gcc/testsuite/ChangeLog
Fixed for 4.6.
Thank you!
(In reply to comment #11) > An example program that shows 3 different methods. Only one works (see comment) > .c = 3, //Will not work > {.c = 3}, //works > {.c= 2}, //Does not work For sake of documentation, with versions from before the fix the second method did not always work. It seems to depend also on the ordering of the members in the anonymous union. If .c is the first, second or third member of the union it seems to work. It does not work for the fourth and following members. Really puzzling.