Absurd declarations, and how gcc deals with them.

Sisyphus sisyphus1@optusnet.com.au
Sat Oct 11 04:42:00 GMT 2008


I have some auto-generated C code that contains declarations like:

    int x[0];
    int y[] = {};

With Microsoft compilers, both lines are illegal. The first produces the 
errors:

  error C2466: cannot allocate an array of constant size 0
  error C2133: 'x' : unknown size

and the second produces the error:

  error C2059: syntax error : '}'

What does gcc-3.x.x do with declarations like that (and why) ?
Is there a case for allowing such declarations to be made, or does Microsoft 
do the right thing by making them errors ?

I wrote the following test script:

-------------------------
#include <stdio.h>

int main(void) {
    int x[0];
    int y[] = {};

    printf("%d\n", y[0]);
    printf("%d\n", x[0]);

    printf("ok\n");
    return 0;
}

-------------------------

And built it (using the MinGW port of gcc-3.4.5) with :
  gcc -o try.exe -Wall try.c

No warnings were issued, and try.exe happily outputs:

1964975285
1965243500
ok

Cheers,
Rob 



More information about the Gcc-help mailing list