Greetings,
I'm writing a program which has multiple source files for global variables (which are in great number) and one header file (MapsThingsRooms.h) to keep the main source file neat and the project organized.
Bellow is the part of the source code that matters modified so I can get my point across more easily.
The MapsThingsRooms.h header file contains the following code:
Code:
#ifndef MapsThingsRooms
#define MapsThingsRooms
// Things inside rooms, eg: a knife
typedef struct thing {
char name[50];
int beenUsed;
int numUses;
} thing;
// Rooms in each map
typedef struct room {
int beenVisited;
int canGoNorth;
int canGoSouth;
int canGoEast;
int canGoWest;
thing thingsInRoom[10];
} room;
// --------------------------------------------------------
// THINGS
extern thing silverKnife;
// ROOMS
extern room s01_11;
#endif
The things.c file contains the following code:
Code:
#include "MapsThingsRooms.h"
thing silverKnife = {
.name = "Silver Knife",
.beenUsed = 0,
.numUses = 0
};
The rooms.c contains the following code:
Code:
#include "MapsThingsRooms.h"
room s01_11 = {
.beenVisited = 0,
.canGoNorth = 0,
.canGoSouth = 1,
.canGoEast = 0,
.canGoWest = 0,
.thingsInRoom[0] = silverKnife
};
Finally, the main.c file contains the following code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include "MapsThingsRooms.h"
int main() {
printf("%s\n", s01_11.thingsInRoom[0].name);
system("pause");
return 0;
}
Now, the problem is: I can't add globally defined things to globally defined rooms, ie, the code I posted above doesn't work because I'm attributing the silverKnife value to s01_11.thingsInRoom[0] and s01_11 is defined globally.
The problem is fixed if I define the s01_11 room inside main(), but, because I have more than 50 rooms, that means I will have 500 lines of room definition inside main(), which is not acceptable.
What can I do?
Also, does C have a boolean type? Using ints as booleans is, in my point of view, not the best practice because it is not immediately clear that the values are supposed to be boolean AND, supposedly, int types are much bigger than boolean types.