static, const, struct woes

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Wed Jun 26 17:23:15 AEST 1991


In article <1991Jun25.170806.23639 at src.honeywell.com>, engstrom at SRC.Honeywell.COM (Eric Engstrom) writes:
> /* This code is outside any block, thus "static". */

None of the things is declared 'static', so they are not so much "static"
as "extern".

>       entity       User_self     = {1, 2};  /* D-1 */
> const entity       Session_self  = {3, 4};  /* D-2 */
> 	entity const Function_self = {5, 6};  /* D-3 */

In these three declarations, the expressions inside the
<initializer-list>s are <constant-expression>s, as they must be at top level.

> entity foo1 = User_self;				/* F-1 */
> entity foo2 = {User_self.cid, User_self.uid};		/* F-2 */

But User_self, User_self.cid, User_self.uid are NOT <constant-expression>s.
What you are trying to do makes sense, and the language could have been
defined so that it would work, but it wasn't.  The initial values given
to top-level variables must be <constant-expression>s.

>     Now I realize that D-1-2-3 are variables whose values are not necessarily
>     defined until run-time, but why should the lines F-1 to F-6 be any
>     different?  I (the compiler) know the amount of memory to allocate in all
>     cases, so why must the initial values be constant?

What has the initial value to do with the amount of memory to allocate?
In most C implementations, the _way_ that top-level and static variables
are initialised is for the compiler and linker to work together so that
already in the object file the right values are in place.  Top level
initialisations do not generate executable code (in most implementations
of C; C++ is different).

>   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.

The 'const' keyword is part of ANSI C (copied from C++).  Sun's documentation
makes it clear that the C compiler that comes with that release of the
operating system is not and is not intended to be an ANSI C compiler.  It's
a "pre-ANSI" (or "Classic") C compiler.
	#if !__STDC__
	#define const
	#endif
will get the code through.
-- 
I agree with Jim Giles about many of the deficiencies of present UNIX.



More information about the Comp.lang.c mailing list