Thread: Array of structs

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    16

    Array of structs

    I am making this MUD-type game and each player struct has two other structs called "gear" and "inventory"

    Gear is what is worn, and that is an array of item-structs (item_t item[11], it is typedef'd)
    Inventory is the same but with a size of 20 instead of 11.

    When I want to load a character, I access the "item ID" from a file (a regular integer) and pass it to a function that should return the correct item, so that it can be applied to the correct inventory/gear slot before the game starts. My problem is that the program crashes as soon as I try to fill these inventory/gear slots... It reads all attributes and strings from the save-file perfectly, so I have narrowed it down to being a definition error (I just started using structs and C)

    This is the stuff:

    Code:
    typedef struct item_s {
      ... all kinds of normal data types
    } item_t;
    
    typedef struct inventory_s {
       item_t* item[20];
    } inventory_t;
    
    typedef struct gear_s {
       item_t* item[MAX_SLOTS]; //#define MAX_SLOTS 11 
    } gear_t
    Then when I try to initialize these, I do this:

    Code:
    if(pos == 13) p->backpack.item[pos-13] = get_item(atoi(buf));
    p is a pointer to a player struct and backpack is an inventory-struct.

    And I do this for every slot. buf is not wrong, that I am certain of.

    Here's get_item(int id):

    Code:
    item_t* get_item(int id) {
       item_t *t;
       strcpy(t->name, "empty");
       return t;
    }
    I havent implemented the ID-thing yet so it just returns an item called "empty".

    Need help badly :|

    This was much easier with classes and "new ()".

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Code:
    item_t* get_item(int id) {
       item_t *t;
       strcpy(t->name, "empty");
       return t;
    }
    What does t point to here?





    (Glances at clock)







    The answer is that it points anywhere! So you're trying to write a string to a pointer of a struct that can't be dereferenced because it's invalid. BOOM.

    Looking at what struct info you've provided, you've got issues. A lot of the time you've declared item_t * instead of item_t. What this means is instead of an array of e.g. 20 item_t's, you've got an array of 20 pointers to item_t's. You would have to allocate memory using malloc() for each one as you go to use them and free() afterwards. I don't think that at this stage you want to do that.

    So... drop the asterisks from your item_t declarations in your structs. Also rewrite get_item() like so:-
    Code:
    void get_item(t_item *t, int id) {
       strcpy(t->name, "empty");
    }
    And call like "get_item(p->backpack.item[pos-13], atoi(buf));"
    Rinse and repeat.
    Last edited by SMurf; 01-01-2007 at 08:02 AM.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    This is where u'r code goes wrong

    Code:
    item_t* get_item(int id) {
        item_t *t;
       strcpy(t->name, "empty");
       return t;
    }
    u havn't allocated space for it. either u can use a malloc. Or use that malloc in a fucntion and call the function which return the address of the allocated space.
    Code:
    item_t* AllocateSpace(void)
    {
         return malloc(sizeof(item_t) * 1);
    }
    And call this function here
    Code:
    item_t *t = AllocateSpace();
    But either SMurf solution would better cos. it reduces complication. You will need this sort of allocation struct when use Linkes structure and many others.

    ssharish2005

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    16
    Your get_item()-function passes an item_t, not a pointer to one.

    Doesn't work :F

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    thats the mistake which u have done. u hanv't allocated space for item_t *. use malloc to do that and then return the pointer back. See mypost. Or look at the SMurf solution.

    ssharish2005

  6. #6
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    I'm feeling charitable today, so here's a complete working listing for you, based on what you've provided.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_SLOTS 11
    
    typedef struct item_s {
        // let's go with what we know
        char name[40];
    } item_t;
    
    typedef struct inventory_s {
       item_t item[20];
    } inventory_t;
    
    typedef struct gear_s {
       item_t item[MAX_SLOTS];
    } gear_t;
    
    typedef struct player_s {
        // amongst other things...
        inventory_t backpack;
    } player_t;
    
    void get_item(item_t *t, int id) {
       strcpy(t->name, "empty");
    }
    
    int main(void)
    {
        char buf[10] = "69";
        int pos = 13;
        player_t *p;
    
        p = malloc(sizeof(*p));
        if (pos == 13)
            get_item(&p->backpack.item[pos-13], atoi(buf));
    
        printf("Item in backpack at index (pos - 13) is: %s\n", p->backpack.item[pos-13].name);
        free(p);
        return 0;
    }
    A quite basic example, but it does illustrate what I wrote earlier. Note that I call malloc() in order to allocate memory for the player pointer and free() it when I am completely finished with it. Have a play with that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  4. array of structs initialization - PLZ help...
    By Vanya in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2002, 08:10 PM
  5. Pointer to Array of Structs
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 08:34 AM