initializing ints with mem refs (ANSI C??)

Ralf Gütlein
Thu Mar 2 02:46:00 GMT 2000

I'm used to use gcc for developing embedded systems' software.
In an ongoing redesign i was urged to use a controller for
which no gcc port is available (NEC 78k0 derivative), so I
switched to the IAR compiler (commercial product). During
porting the software to the new target, I ran into several
problems. All of them could be solved but one:

In my source code I extensively used constructs like

const int object = 0;
int a[2] = {1, (const int)&object;};

(I know this is not portable when the internal representation of
addresses is other than the size of int. But it lead to a very
elegant and code efective solution to my coding problem.)

But instead of issueing a warning (sth. like 'loosing
precision') the IAR compiler complains about 'constant
initializer expected' and aborts.
Gcc swallowed the code with no problems.

My question:

What does the ANSI standard demand here? In my eyes the
initializer **is** constant (i.e. can be evaluated at compile
time). The same IAR compiler does not moan when I use

int * const a[2] = {(int *const)1, &object };


Is ANSI precise here? Or is gcc's implemenation more relaxed
than IAR's?



More information about the Gcc-help mailing list