As the title says, can it be done?
struct Room{
char *type; //Lecture hall, laboratory, etc.
char *name;
int *capacity; //How may people it can hold
struct Building *building;
};
struct Building{
char *name;
char *type; //Administrative, school, etc.
int *rooms; //How many does it have
int *total_capacity; //Total capacity of the rooms
struct Room *room;
};
I'm trying to kind of simulate a database using struct
arrays to simulate tables.
As it can be seen above, I have a "table" of buildings and another one for rooms. Each building has a list of pointers to its rooms, and each room needs a pointer to the building that it is situated in.
And here comes the problem, whichever is defined first will complain that the other is not defined, which, for functions, could easily be solved with using a prototype, but I can't find anything of the sort for struct
I tried things like
struct Building;
struct Building();
struct Building{};
struct Building(){};
Along with the typedef
variation but nothing seems to work. Is there such a thing as prototyping for structs? If not, what is the workaround, if any?
2 Answers 2
struct Building;
should have worked:
struct Building;
struct Room{
char *type; //Lecture hall, laboratory, etc.
char *name;
int *capacity; //How may people it can hold
struct Building *building;
};
struct Building{
char *name;
char *type; //Administrative, school, etc.
int *rooms; //How many does it have
int *total_capacity; //Total capacity of the rooms
struct Room *room;
};
-
Thank you, it worked in the end, I had a function that used
Building
in between the 2 definitions that was causing all the fuss. Much appreciated. ^^Rares Dima– Rares Dima2016年12月02日 02:11:52 +00:00Commented Dec 2, 2016 at 2:11
Jerry's answer will fix your immediate problem, but I think you should reconsider your data model. Circular relationships are almost always bad juju, and should be avoided whenever possible.
I think you should consider creating a third structure that associates rooms and buildings to each other, something like:
struct Room {
// room-specific data
};
struct Building {
// building-specific data
};
struct BuildingRoomsAssoc {
struct Building *building;
struct Room *room;
};
and have a unique entry for each building and room mapping:
struct BuildingRoomAssoc buildingRoomMap[N];
...
struct Building *b = malloc( sizeof *b );
b->name = strdup( "The Hilton Plaza" );
b->type = strdup( "swanky hotel" );
struct Room *r = malloc( sizeof *r );
r->name = strdup( "Grand Ballroom" );
r->type = strdup( "swanky ballroom" );
r->capacity = 200;
buildingRoomMap[i].building = b;
buildingRoomMap[i].room = r;
i++;
struct Room *r = malloc( sizeof *r );
r->name = strdup( "Brazos Room" );
r->type = strdup( "small meeting room" );
r->capacity = 20;
buildingRoomMap[i].building = b;
buildingRoomMap[i].room = r;
i++;
So, you can search buildingRoomMap
for a specific room and find the corresponding building, or for a specific building and find all the corresponding rooms. You'll want to come up with a more sophisticated structure than a flat array for your associative class, but this should at least point you in the right direction.
Explore related questions
See similar questions with these tags.
struct Building; struct Room;
. Subsequently you can declare pointers to either struct as needed. Typedefs will neither help nor hurt; it's the forward declarations that will help.