Thread: Can't have Global Struct inside Global Struct

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    Can't have Global Struct inside Global Struct

    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.
    Last edited by Pulse Cloud; 10-05-2011 at 03:04 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've got to have them somewhere. Why does it matter where? Make a function, do your initialization there. Better yet, make a file, store the definitions in text, and parse it at runtime. C99 introduced _Bool and associated macros. Include <stdbool.h> to use them.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    @quzah
    Yeah, but I want them to be global so I don't have to pass them to functions as arguments. That's the whole point.

    Thank you for stdbool.h!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually your compiler should be complaining about redefinitions... (Set your warning level to maximum and recompile...)

    Code:
     
    // Things inside rooms, eg: a knife
    typedef struct thing {   <--- thing created here
        char name[50];
        int beenUsed;
        int numUses;
    } thing;     <-- thing redefined here
    
    // Rooms in each map
    typedef struct room {   <-- room created here
        int beenVisited;
        int canGoNorth;
        int canGoSouth;
        int canGoEast;
        int canGoWest;
    
        thing thingsInRoom[10]; 
    } room;   <--- room redefined here.
    Try it like this....
    Code:
    // Things inside rooms, eg: a knife
    typedef struct t_thing {
        char name[50];
        int beenUsed;
        int numUses;
    } thing;
    
    // Rooms in each map
    typedef struct t_room {
        int beenVisited;
        int canGoNorth;
        int canGoSouth;
        int canGoEast;
        int canGoWest;
    
        thing thingsInRoom[10];
    } room;
    The t_ means it's a "tag" name... it's of no significance other than to prevent the compiler from failing at the end of the typedef.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Having global data doesn't mean you can't initialize them locally some place. If you really want everything global, then you need to make sure all of your things are defined before your rooms. Is there any reason you aren't putting the initialization in the same file as the declaration?
    Code:
    struct foo { ... } data[ SIZE ] = {
        { ... first foo },
        { second foo },
        ...
        { Nth foo },
    };

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    @CommonTater: Thank you. I've updated my code.


    Quote Originally Posted by quzah View Post
    Having global data doesn't mean you can't initialize them locally some place. If you really want everything global, then you need to make sure all of your things are defined before your rooms.
    I've encapsulated the things inside a void createThings(), did the same for the rooms (createRooms), made the variables extern, added the functions to the header and added them to main, first createThings then createRooms.
    It still doesn't work.

    Is there any reason you aren't putting the initialization in the same file as the declaration?
    What do you mean?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  2. global struct variable best use
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 06-05-2009, 05:08 AM
  3. Global struct in MFC app
    By MyglyMP2 in forum Windows Programming
    Replies: 3
    Last Post: 08-29-2008, 03:41 AM
  4. My struct is global, isn't?
    By Joelito in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2006, 08:43 PM
  5. need help acessing a global struct in function.
    By bobish in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2002, 10:32 AM