Red Hat Bugzilla – Bug 43114
initializing constants from each other
Last modified: 2007-04-18 12:33:30 EDT
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux 2.4.2-2smp i686; en-US; 0.8.1)
Description of problem:
gcc-2.96-81 refuses to initialize one const int with an expression
containing the value of another constant integer, exiting with the error
"initializer element is not constant".
Steps to Reproduce:
1.Save attached file as "testconst.c"
2."gcc -o testconst testconst.c"
Actual Results: testconst.c:3: initializer element is not constant
Expected Results: Successful compilation
egcs-1.1.2 exits with the same error. According to the (year old) gcc-bugs
post archived at the above URL, gcc-2.95.2 compiles this code as expected.
I'm not 100% certain that initializing one const from another is valid
ANSI C (my copy of K doesn't bring up the problem), but I can't see why it
wouldn't be allowed.
Created attachment 20050 [details]
minimal demonstration source code
From my testing gcc-2.95.2 does not accept it either.
An integer constant expression shall have integer type and shall only have
operands that are integer constants, enumeration constants, character
constants, sizeof expressions whose results are integer constants, and
floating constants that are the immediate operands of casts. Cast operators
in an integer constant expression shall only convert arithmetic types to
integer types, except as part of an operand to the sizeof operator.
More latitude is permitted for constant expressions in initializers. Such a
constant expression shall be, or evaluate to, one of the following:
- an arithmetic constant expression,
- a null pointer constant,
- an address constant, or
- an address constant for an object type plus or minus an integer constant
An implementation may accept other forms of constant expressions.
All the expressions in an initializer for an object that has static storage
duration shall be constant expressions or string literals.
(only included the relevant paragraphs).
As you can see, your program is not valid ISO C, but according to 6.6.10 gcc
could decide to accept it as constant expression, but it does not have to
(unlike in C++).
None of egcs 1.1.x, gcc 2.95.x, gcc-2.96-RH, gcc 3.0 or gcc 3.1 accept this.