valid c code? 
Author Message
 valid c code?

Hey,

        I came across this snippet of C code and I was wondering if it was
valid:
        int x = 2;
        int y = 4;
        int array[5] = {0, 1, x+y, x, y, 5};

It compiles under gcc -ansi and Visual Studio, but not under Metrowerks
Codewarrior.  I tried looking through K&R and wading through their
explanation of the relevant pieces of the grammar, but I really couldn't
make much sense out of it.  Is this a compiler bug?

Thanks,
Jan


      Class of 2000                  "Save a tree.  Eat a gopher."

--



Sun, 31 Mar 2002 03:00:00 GMT  
 valid c code?


Quote:

>Hey,

>     I came across this snippet of C code and I was wondering if it was
>valid:

Well, C9X passed it's FDIS ballot.  And this is conforming ISO/IEC 9899:1999
(at least at block scope).

Quote:
>     int x = 2;
>     int y = 4;
>     int array[5] = {0, 1, x+y, x, y, 5};

This is not conforming ISO/IEC 9899:1990 (since I don't thinkt he above
document has been formally published yet).
--



Sun, 31 Mar 2002 03:00:00 GMT  
 valid c code?


Quote:

> I came across this snippet of C code and I was wondering if it was
> valid:
>    int x = 2;
>    int y = 4;
>    int array[5] = {0, 1, x+y, x, y, 5};

This is not valid in C89 - initializers for arrays must be constant
expressions.

Sent via Deja.com http://www.deja.com/
Before you buy.
--



Sun, 31 Mar 2002 03:00:00 GMT  
 valid c code?


Quote:
>I came across this snippet of C code and I was wondering if it was
>valid:
>    int x = 2;
>    int y = 4;
>    int array[5] = {0, 1, x+y, x, y, 5};

It is not valid for two reasons:
  - there are 6 initializers for an array of 5 ints
  - two of the initializers are not constant

Quote:
>It compiles under gcc -ansi ...

"gcc -ansi" is not an ANSI C compiler.  "gcc -ansi -pedantic" is an
ANSI C compiler (modulo any bugs).  With -pedantic, gcc diagnoses
the non-constant initializers correctly.

(I suspect the initializers are legal in C99.)
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc


--



Sun, 31 Mar 2002 03:00:00 GMT  
 valid c code?

Quote:
>    int x = 2;
>    int y = 4;
>    int array[5] = {0, 1, x+y, x, y, 5};

seems strange that the compiler accepts an array with a length of 5 elements
when you assign six to it...

But I suppose you were hinting at the problem with the x's and y's?
I wouldn't know about that one...
--
Regards, Maarten 'Elmer' Egmond.
----------Be a friend to the Earth, and it will be a friend to you----------

--



Sun, 31 Mar 2002 03:00:00 GMT  
 valid c code?

# Hey,

#       I came across this snippet of C code and I was wondering if it was
# valid:
#       int x = 2;
#       int y = 4;
#       int array[5] = {0, 1, x+y, x, y, 5};

# It compiles under gcc -ansi and Visual Studio, but not under Metrowerks
# Codewarrior.

You need gcc's -pedantic as well to make it behave *really* close to a
Standard C compiler. I recommend -Wall in addition.


tst.c: In function `main':
tst.c:7: initializer element for `array[2]' is not computable at load time
tst.c:7: initializer element for `array[3]' is not computable at load time
tst.c:7: initializer element for `array[4]' is not computable at load time
tst.c:7: warning: excess elements in array initializer after `array'

# I tried looking through K&R and wading through their
# explanation of the relevant pieces of the grammar, but I really couldn't
# make much sense out of it.  Is this a compiler bug?

The Standard says (6.5.7 Initialization):

<QUOTE>
Constraints

 There shall be no more initializers in an initializer list than there
 are objects to be initialized. [Note the last warning -- JS]

 ...

 All the expressions in an initializer for an object that has static
 storage duration or in an initializer list for an object that has
 aggregate [array or structure -- JS] or union type shall be constant
 expressions.
</QUOTE>

x+y, x and y are not constant expressions, hence a diagnostic is
required (constraint violations must be diagnosed). The same applies to
the 6th initializer for the 5 element array. A compiler that can not be
made to issue similar diagnostics can be considered broken. You may need
a bunch of options to force the required diagnostics, as gcc's case
demonstrates.

# Thanks,
# Jan

You're welcome.

Regards,

        Jens
--
Jens Schweikhardt  http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)
--



Sun, 31 Mar 2002 03:00:00 GMT  
 valid c code?

Quote:



>>I came across this snippet of C code and I was wondering if it was
>>valid:
>>        int x = 2;
>>        int y = 4;
>>        int array[5] = {0, 1, x+y, x, y, 5};

>It is not valid for two reasons:
>  - there are 6 initializers for an array of 5 ints
>  - two of the initializers are not constant

     ^^^
     Three?

     I originally heard this as referring to physicists, but if the
shoe fits:

     There are three types of programmers: those who can count and
those who can't.

[snip]

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
     I have preferences.
     You have biases.
     He/She has prejudices.
--



Mon, 01 Apr 2002 03:00:00 GMT  
 valid c code?


Quote:
> >>   int array[5] = {0, 1, x+y, x, y, 5};

> >It is not valid for two reasons:
> >  - there are 6 initializers for an array of 5 ints
> >  - two of the initializers are not constant
>      ^^^
>      Three?

Nah. Zero based. Initializer 0, 1 and 2 => two. ;-)

Greetings from
 _____
 /_|__| Auke Reitsma, Delft, The Netherlands.
/  | \  -------------------------------------
        Remove NO_SPAM_ from my address ...
--



Tue, 02 Apr 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. VC71 final beta - Reject valid C++ code snippet

2. Who thinks this is valid C code?

3. Is this valid C++ code?

4. Showing Call Stack and Determine which object is not valid LARGE CHUNK OF CODE

5. is this valid code?

6. Is this valid code?

7. Is this a valid programming technique ?

8. Is fclose(NULL) a valid call ?

9. Specified cast is not valid

10. Pointer comparison with == and != valid?

11. Is 300F a valid float constant?

12. Is this valid C...?

 

 
Powered by phpBB® Forum Software