Confusion in initialiser list.

Robert Harley Robert.Harley@inria.fr
Fri Apr 16 01:45:00 GMT 1999


Joerg Czeranski posted this to usenet:

------------------------------------------------------------------------------
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.)
------------------------------------------------------------------------------


Obviously there's a "*" missing but consider it as is.
The output code looks like:

------------------------------------------------------------------------------
weekday:
        .ascii "Sun\0"
        .zero   3
------------------------------------------------------------------------------


It might therefore appear that the list is being taken for a
comma-expression.  But if parentheses are added to force a
comma-expression like this:

------------------------------------------------------------------------------
char weekday[7] = { ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") };
------------------------------------------------------------------------------

then it warns:

------------------------------------------------------------------------------
delme.c:1: initializer element for `weekday[0]' is not constant
------------------------------------------------------------------------------

whereas that warning didn't appear before.  So the code presumably led
to some other mix up.  Dunno what's going on...

Bye,
  Rob.


More information about the Gcc-bugs mailing list