static, const, struct woes
Dale Worley
worley at compass.com
Thu Jun 27 01:30:26 AEST 1991
In article <1991Jun25.170806.23639 at src.honeywell.com> engstrom at SRC.Honeywell.COM (Eric Engstrom) writes:
entity User_self = {1, 2}; /* D-1 */
const entity Session_self = {3, 4}; /* D-2 */
entity const Function_self = {5, 6}; /* D-3 */
entity foo1 = User_self; /* F-1 */
entity foo2 = {User_self.cid, User_self.uid}; /* F-2 */
entity foo3 = Session_self; /* F-3 */
entity foo4 = {Session_self.cid, Session_self.uid}; /* F-4 */
entity foo5 = Function_self; /* F-5 */
entity foo6 = {Function_self.cid, Function_self.uid}; /* F-6 */
What difference (if any) is there between the three lines marked D-1, D-2,
and D-3?
D2 and D3 define constant objects, which you aren't allowed to assign to.
Why, if either D-2 or D-3 are truly legal, do the lines F-3 to F-6 all
fail to compile with the following error:
"initializer for static variable is not constant"
Because the values of the expressions "Session_self", etc. aren't
"constant expressions". In particular, to figure out what the value
is, a fetch from memory would have to be done. Of course, from the
code above, the compiler could figure out what the fetch would yield,
but most compilers aren't that clever, and ANSI C doesn't require them
to be.
Note that the Sun C compiler (OS 4.1.1) gave more, and more
cryptic, errors,
even complaining about the "const" portion of D-1-2-3.
Probably because the Sun C compiler doesn't implement 'const', and
thus thinks that 'const' is an identifier that you're trying to
declare.
Dale Worley Compass, Inc. worley at compass.com
--
BYOB if you like, but please don't let yourself go farther than you
can return. Believe me, you're going to need most of those internal
organs in the morning.
More information about the Comp.lang.c
mailing list