Expressions in initializers

Christopher R Volpe volpe at camelback.crd.ge.com
Mon Mar 4 23:33:25 AEST 1991


In article <760 at ajpo.sei.cmu.edu>, rabbieh at ajpo.sei.cmu.edu (Harold
Rabbie) writes:
|>Here's one for the comp.lang.c.lawyers - K&R 2 says (without explanation)
|>that non-constant expressions can be used as initializers only for static 
|>scalars, not for automatics, and not for aggregates.

Read it again. You have it almost backwards.

|>
|>e.g. I can say:
|>
|>static double x = sqrt( 2.0 );

No you can't.

|>
|>but I can't say:
|>
|>void foo( void ) 
|>{
|>    double x = sqrt( 2.0 );
|>}

Yes you can.

|>
|>nor can I say:
|>
|>static struct foo {
|>    double x;
|>} bar = { sqrt( 2.0 ) };

That's correct.

|>
|>What's the deal here - is ANSI easing up on those no-good implementers :-)
|>or is there a valid reason for this restriction?
|>
|>P.S. No need to FAQ me over on this one.

Statics are initialized at compile time, therefore they can't be initialized
by an expression that can be evaluated only at runtime.
                         
==================
Chris Volpe
G.E. Corporate R&D
volpecr at crd.ge.com



More information about the Comp.lang.c mailing list