Expressions in initializers

Hans Mulder hansm at cs.kun.nl
Fri Mar 8 08:34:31 AEST 1991


In article <3599.27d3ca8a at iccgcc.decnet.ab.com> browns at iccgcc.decnet.ab.com (Stan Brown) writes:
>So a conforming compiler is free to consider sqrt(2.0) to be a
>compile-time constant.

Nope.  The ANSI standard gives an exhaustive list of operators that
can appear in a "constant expression" as required in an aggregate
initialiser and function calls are not on the list.

Consequently,

	struct silly { double d; } foo = { sqrt(2.0) };

is illegal.  This has nothing to do with the question of whether the
optimiser is allowed to replace

	foo.d = sqrt(2.0);
by
	foo.d = 1.4142135623730950488;


Also missing from the list is the array subscript operator, thus

	struct silly { char ch; } foo = { "hello"[3] };

is illegal, the fact that "hello"[3] == 'l' notwithstanding.


Have a nice day,

Hans Mulder	hansm at cs.kun.nl



More information about the Comp.lang.c mailing list