Initilization of global structures

Wayne H Scott wscott at EN.ECN.PURDUE.EDU
Wed May 2 14:23:02 AEST 1990


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 at en.ecn.purdue.edu
Electrical Engineering  |  BITNET:     wscott%ea.ecn.purdue.edu at purccvm
Purdue University       |  UUCP:      {purdue, pur-ee}!en.ecn.purdue.edu!wscott
_______________________________________________________________________________
				"To iterate is human.  To recurse, divine."



More information about the Comp.lang.c mailing list