-
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 ()".
-
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.
-
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
-
Your get_item()-function passes an item_t, not a pointer to one.
Doesn't work :F
-
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
-
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.