From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,cb40752a27f3d651 X-Google-Attributes: gid1164f8,public X-Google-Thread: 1014db,cb40752a27f3d651 X-Google-Attributes: gid1014db,public X-Google-Thread: fee84,cb40752a27f3d651 X-Google-Attributes: gidfee84,public From: Joerg Czeranski Subject: Re: comma operator: C compiler bug or bogus usage? Date: 1999/04/15 Message-ID: <5u8PBMkHV$73G$1@joerch.org>#1/1 X-Deja-AN: 466825603 References: <7f567v$sp1$1@news.iastate.edu> Organization: Joerch Software Newsgroups: comp.lang.c,comp.unix.osf.osf1,comp.sys.dec John Hascall wrote in <7f567v$sp1$1@news.iastate.edu>: > # uname -a > OSF1 pvtest.cc.iastate.edu V4.0 878 alpha > > # cat ct.c > #include > > int foo[(3,4)]; > int bar[3,4]; > > 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 "]". > int bar[3,4]; > ---------^ > cc: Error: ct.c, line 3: In the declaration of "foo", "3,4" is not constant, but occurs in a context that requires a constant expression. > int foo[(3,4)]; > ---------^ > > It seems to me that the first error message is clearly not helpful, > but what haven't been able to discover is: > > Is the result of: > > constant-expression comma-operator constant-expression > > still a constant-expression or not? If not, why not (and it > seems to me this is the only operator with this property?) > > John > PS, FWIW, gcc accepts both of these and produces the 'expected' results I just checked a draft of C9X (I don't have the current ANSI/ISO standard), and it appears to me that the compiler is right: "[ ]" contains an assignment-expression, not a general expression (section 6.5.5: Declarators). Therefor "int bar[3],..." is valid, but "int bar[3,..." not, and it's not unreasonable for the compiler to complain about the missing "]" that would enable it to continue parsing. And: Constant expressions shall not contain comma operators (section 6.4: Constant expressions). (C9X allows variable-size arrays though, so "int foo[(3,4)]" should then be ok.) gcc has a different handling of "," and IMHO it violates the standard in that respect. Even gcc -ansi -pedantic doesn't emit a diagnostic for this error (it violates the constraint in 6.3.16.1: Simple assignment, referred to by 6.5.8: Initialization): char weekday[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; (That's an actual example of something a friend who is just learning C stumbled over.) joerch