From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1014db,cb40752a27f3d651 X-Google-Attributes: gid1014db,public X-Google-Thread: 1164f8,cb40752a27f3d651 X-Google-Attributes: gid1164f8,public X-Google-Thread: fee84,cb40752a27f3d651 X-Google-Attributes: gidfee84,public From: kaz@ashi.FootPrints.net (Kaz Kylheku) Subject: Re: comma operator: C compiler bug or bogus usage? Date: 1999/04/15 Message-ID: #1/1 X-Deja-AN: 466811440 References: <7f567v$sp1$1@news.iastate.edu> X-Trace: newsgate.direct.ca 924201220 204.239.179.1 (Thu, 15 Apr 1999 11:33:40 PDT) Organization: Psycho-Neurotic Institute for The Very, Very Nervous NNTP-Posting-Date: Thu, 15 Apr 1999 11:33:40 PDT Newsgroups: comp.lang.c,comp.unix.osf.osf1,comp.sys.dec On 15 Apr 1999 17:05:03 GMT, John Hascall wrote: ># uname -a >OSF1 pvtest.cc.iastate.edu V4.0 878 alpha > ># cat ct.c >#include > >int foo[(3,4)]; >int bar[3,4]; Both of these declare array objects with one dimension, having four elements. >main(){ printf("sizes of foo and bar are %d, %d\n", sizeof(foo), sizeof(bar)); } > ># cc -g -O0 ct.c -o ct >cc: Error: ct.c, line 4: Missing "]". Ah, you are being bitten by the fact that 3, 4 is not a constant expression. Though this is a strange way to report the required diagnostic. :) The expressions which specify array dimensions must be constant. Expressions that have a comma operator in them are not considered constant expressions, even though both of the constituents may be constant expressions. >int bar[3,4]; > > constant-expression comma-operator constant-expression > > still a constant-expression or not? If not, why not (and it It's certainly an expression with a value that can be determined at compile time, no argument there. However, that is not the same as a constant expression. The latter is just a term that is arbitrarily defined in the C standard. You have to apply that definition to determine whether something is a constnat expression or not. >John >PS, FWIW, gcc accepts both of these and produces the 'expected' results It does not if you tell it to be an ANSI C compiler witn -ansi -pedantic. I tried it with egcs 2.91.57, which accepts it without the -pedantic option, but produces the following warning if you turn on -pedantic: warning: ANSI C forbids variable-size array 'a'. You have fallen into a common pitfall by assuming that your compiler is a C compiler by default. Many compilers default to supporting their implementor's extensions, with ANSI C support being a negative option. For example, the Microsoft compiler supports transparent unions and // comments by default. GCC supports // comments, statement blocks that behave like expressions, array sizes that are computed at run time, nested functions and other goodies.