Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!uunet!munnari.oz.au!bruce!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: static, const, struct woes Message-ID: <6517@goanna.cs.rmit.oz.au> Date: 26 Jun 91 07:23:15 GMT References: <1991Jun25.170806.23639@src.honeywell.com> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 46 In article <1991Jun25.170806.23639@src.honeywell.com>, engstrom@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 s are 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 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 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.