This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Not conform to c90?


On Tue, Oct 04, 2011 at 04:09:03AM -0700, 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.

No, it says that zero-length arrays are not allowed in C90, so if you
want to be compliant with C90 you would have to use an array of length
1 instead.  (Normally one uses zero-length arrays to fake a variable
length array.  Technically this trick is not actually allowed by C90
even if one uses a 1-length array, but so far I have not heard of any
single C implementation where it does not work in practice.)


> 
> 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
> 
> 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?

No, it means a program using zero-length arrays does not conform to C90
(or C99 for that matter) so as far as the C standard is concerned GCC
may do whatever it wants if it encounters a zero-length array.



-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013@student.uu.se


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]