gcc erroneously reports "excess elements in array initializer" under certain circumstances (sample code attached). I'm using gcc 3.3.3 under Fedora Core 2, but I've seen the same problem on 3.4 and 4.0 experimental.
---- begin self-contained test program bug.c ---- /* I know you don't want #includes, but this is here just for the * sake of printf(). It can probably be safely removed. */ #include <stdio.h> /**************************************************************** * This array is okay. ****************************************************************/ int *a[] = { (int []) { 1, 2, 3, 4, 0 }, (int []) { 5, 6, 0 }, (int []) { 7, 8, 9, 10, 11, 12, 13, 0 }, (int []) { 14, 0 } }; /**************************************************************** * This array should be identical. It contains all the same * elements as a[], but this time the size (4) is specified. * Gcc generates "excess element" warnings for each sub-array * with more than 4 elements, even though the length of these * arrays has NOT been specified. ****************************************************************/ int *b[4] = { (int []) { 1, 2, 3, 4, 0 }, /* WARNING: excess elements in array initializer */ (int []) { 5, 6, 0 }, (int []) { 7, 8, 9, 10, 11, 12, 13, 0 }, /* WARNING: excess elements in array initializer */ (int []) { 14, 0 } }; /**************************************************************** * This array is the same as b[], except the size of each sub- * array is specified. It compiles without warnings and looks * the same as a[] when passed to print_array(). ****************************************************************/ int *c[4] = { (int [5]) { 1, 2, 3, 4, 0 }, (int [3]) { 5, 6, 0 }, (int [8]) { 7, 8, 9, 10, 11, 12, 13, 0 }, (int [2]) { 14, 0 } }; /**************************************************************** * Not only does the compiler throw warnings, it also produces * different code. On my machine, print_array() produces the * same output from a[] and c[], but not from b[]. ****************************************************************/ void print_array (int **ptr, int count) { int i, j; for (i=0; i < count; i++) for (j=0; ptr[i][j]; j++) printf ("%d ", ptr[i][j]); printf ("\n"); } int main (int argc, char **argv) { print_array (a, sizeof (a) / sizeof (*a)); print_array (b, sizeof (b) / sizeof (*b)); print_array (c, sizeof (c) / sizeof (*c)); return 0; } ---- end self-contained test program bug.c ---- Here is the gcc command line. Where should I attach the -save-temps output files? # gcc -v -save-temps bug.c Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/specs Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --disable-libunwind-exceptions --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux Thread model: posix gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/cc1 -E -quiet -v -D__GNUC__=3 -D__GNUC_MINOR__=3 -D__GNUC_PATCHLEVEL__=3 bug.c bug.i ignoring nonexistent directory "/usr/i386-redhat-linux/include" #include "..." search starts here: #include <...> search starts here: /usr/local/include /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/include /usr/include End of search list. /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/cc1 -fpreprocessed bug.i -quiet -dumpbase bug.c -auxbase bug -version -o bug.s GNU C version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) (i386-redhat-linux) compiled by GNU C version 3.3.3 20040412 (Red Hat Linux 3.3.3-7). GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=129392 bug.c:23: warning: excess elements in array initializer bug.c:23: warning: (near initialization for `(anonymous)') bug.c:25: warning: excess elements in array initializer bug.c:25: warning: (near initialization for `(anonymous)') bug.c:25: warning: excess elements in array initializer bug.c:25: warning: (near initialization for `(anonymous)') bug.c:25: warning: excess elements in array initializer bug.c:25: warning: (near initialization for `(anonymous)') bug.c:25: warning: excess elements in array initializer bug.c:25: warning: (near initialization for `(anonymous)') as -V -Qy -o bug.o bug.s GNU assembler version 2.15.90.0.3 (i386-redhat-linux) using BFD version 2.15.90.0.3 20040415 /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/collect2 --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/../../../crt1.o /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/../../../crti.o /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/crtbegin.o -L/usr/lib/gcc-lib/i386-redhat-linux/3.3.3 -L/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/../../.. bug.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/crtend.o /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/../../../crtn.o -- Summary: Erroneous "excess elements in array initializer" warning Product: gcc Version: 3.3.3 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: acct4290 at dedion dot com CC: gcc-bugs at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21033