[comp.lang.c] Initilization of global structures

wscott@EN.ECN.PURDUE.EDU (Wayne H Scott) (05/02/90)

I am having some problems with a program I am writing.  This might be very
simple but I can't figure it out.

I have several global arrays that hold constant information as arrays of 
structures. (weapon data, race data, ...)  I also have a global array of player
information that has some arrays of number of weapons.  The problem I am having
is that I want the compiler to be able to figure out how many weapons are 
defined in the structure and use that number to define the player structure.

Perhaps an example will help.  The following code does not work on my machine
but it explains what I want.

[file=globals.c]
	#define MAXNUM 5
	struct weapon {
		char *name;
		/* etc */
	};
	struct weapon wstats[] = { "knife", "sword" /* etc ... */ };

	#define NUMWEAP (sizeof(wstats)/sizeof(struct weapon))
/* or perhaps
	int NUMWEAP = (sizeof(wstats)/sizeof(struct weapon));
*/
	struct person {
		char *name;
		int weaps[NUMWEAP];
	};
	struct person party[MAXNUM];

[file=main.h]
	extern struct weapon {
		char *name;
	} wstats[];

	#define NUMWEAP (sizeof(wstats)/sizeof(struct weapon))

	extern struct person {
		char *name;
		int weaps[NUMWEAP];
	} party[];

The globals.c file works but I can't get the main.h file correct so that other
files can see the information.

If someone can figure out what I am trying to say and knows a solution please
send me mail.

Sorry if this is trivial.

_______________________________________________________________________________
Wayne Scott             |  INTERNET:   wscott@en.ecn.purdue.edu
Electrical Engineering  |  BITNET:     wscott%ea.ecn.purdue.edu@purccvm
Purdue University       |  UUCP:      {purdue, pur-ee}!en.ecn.purdue.edu!wscott
_______________________________________________________________________________
				"To iterate is human.  To recurse, divine."

schaefer@ogicse.ogi.edu (Barton E. Schaefer) (05/02/90)

In article <9005020423.AA05715@en.ecn.purdue.edu> wscott@EN.ECN.PURDUE.EDU (Wayne H Scott) writes:
} 
} I am having some problems with a program I am writing.  This might be very
} simple but I can't figure it out.
} 
} [file=globals.c]
} 	#define MAXNUM 5
} 	struct weapon {
} 		char *name;
} 		/* etc */
} 	};
} 	struct weapon wstats[] = { "knife", "sword" /* etc ... */ };

This is not doing what you think it's doing.  The outer set of { } defines
the initializer for the array.  You need an additional set of { }  for each
structure-typed element.  Try:

	struct weapon wstats[] = {
		{ "knife", /*etc*/ }, { "sword", /*etc*/ }, /* etc ... */
	};
-- 
Bart Schaefer         |  The Ultimate Scheme:         [apologies to Wadler]
                      |  call-cc (lambda (answer)
schaefer@cse.ogi.edu  |      (/ universe (if (= life 0) (answer 42) life)))

g3f@mentor.cc.purdue.edu (Wayne Scott) (05/03/90)

In article <9005020423.AA05715@en.ecn.purdue.edu> wscott@EN.ECN.PURDUE.EDU (Wayne H Scott) writes:
>
>I am having some problems with a program I am writing.  This might be very
>simple but I can't figure it out.
>
>I have several global arrays that hold constant information as arrays of 
>structures. (weapon data, race data, ...)  I also have a global array of player
>information that has some arrays of number of weapons.  The problem I am having
>is that I want the compiler to be able to figure out how many weapons are 
>defined in the structure and use that number to define the player structure.
>

Perhaps I should have been more clear about what my problem is.  I have no
problem specifing the starting values for the array.  My problem is that I don't
want to have a fixed constant for the number of elements in the array.  I want
the compiler to figure that out.  The sizeof trick I used in the posting does
that for the globals.c file but the number is not a constant so I can define
another structure with a fixed sized array in it.

I was sent one reasonable way using alot of preprocessor tricks, but it seems
like it should be easier.

Wayne Scott
wscott@en.ecn.purdue.edu