On ecgs (gcc) 2.91.57 and on gcc 3.2.3 I am getting the
error "initializer element is not constant" on the below
code. The code compiles fine with other compilers I
have. Is this valid C89 code?
No, it is not. Initializers for objects with static storage duration,
and that includes all objects defined at file scope, must be constant
expressions. You are trying to initialize the pointer 'a' with the
value of another object. The value of any object, even if it is a
const qualified object, is not a constant expression in C.
extern int *b;
int *a = b;
int main(void)
{
return (0);
}
Even this is not (necessarily) valid C:
int x;
int *const b = &x; /* valid, &x is an address constant */
int *a = b; /* not valid */
The initialization of 'a' is not valid even though the value of 'b' is
known at compile time, which it is not in your case. The value of an
object is just not among the defined constant expressions in the C
standard.
It is actually possible for a compiler to accept this as an extension,
that being the reason for the (necessarily) I wrote above, because the
C standard allows an implementation to "accept other forms of constant
expressions". But I don't know of any implementation that does.