On 4 October 2011 12:09, Bingfeng Mei wrote:
> Hello,
> According to
> http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Zero-Length.html#Zero-Length
> A zero-length array should have a length of 1 in c90.
I think you've misunderstood that page. You cannot have a zero-length
array in C90, what that page says is that in strict C90 you would have
to create an array of length 1 as a workaround. It's not saying
sizeof(char[0]) is 1.
GNU C an C99 allow you to have a zero-length array.
> But I tried
>
> struct
> {
> char a[0];
> } ZERO;
>
> void main()
> {
> int a[0];
> printf ("size = %d\n", sizeof(ZERO));
> }
>
> Compiled with gcc 4.7
> ~/work/install-x86/bin/gcc test.c -O2 -std=c90
>
> size = 0
If you add -pedantic you'll discover that program isn't valid in C90.
> I noticed the following statement in GCC document.
> "As a quirk of the original implementation of zero-length arrays,
> sizeof evaluates to zero."
>
> Does it mean GCC just does not conform to c90 in this respect?
C90 doesn't allow zero length arrays, so you're trying to evaluate a
GNU extension in terms of a standard. I'm not sure what you expect to
happen.