static, const, struct woes

Eric Engstrom engstrom at SRC.Honeywell.COM
Wed Jun 26 03:08:06 AEST 1991


I have several questions regarding the following code:

/* -------------------------------------------------- */
/* This code is outside any block, thus "static". */

typedef struct {
   long cid;
   long uid;
 } entity;

	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 */

/* -------------------------------------------------- */

Q1) What difference (if any) is there between the three lines marked D-1, D-2,
    and D-3?  All are acceptable to my compiler (gcc 1.39 - Sun Sparc).
    (I did look at K&R-2 and the FAQ, but there was not enough meat for my
    taste ;-)

Q2) 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"

    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?

Q3) How must I change my definitions (if possible) to accommodate lines
    D-1-2-3 being located in different code files and having "extern"
    references from this file?

  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.


Actually, you can replace my struct definition with simple "int" declarations
and the same errors occur.

Eric
+---------
Eric Engstrom, Honeywell SRC		   | The verb form of 'committee' 
Inter: engstrom at src.honeywell.com	   |  is 'commit', as in 
UUCP:  {umn-cs,ems,bthpyd}!srcsip!engstrom |  "the committe was committed
MAIL: 3660 Technology Drive, Mpls, MN      |  to the Anoka Mental Hospital."



More information about the Comp.lang.c mailing list