I am having trouble getting a nested array of structs to retain its members after returning from a function call.
Here is what I have defined in the header files (condensed struct members for readability):
typedef struct {
char *name;
} weapon;
#define MAX_SHOP_ITEMS 10
struct shop {
weapon items[MAX_SHOP_ITEMS];
};
typedef struct {
char *name;
struct shop shop;
} location;
I use these functions to add members to the shop.items[] array of structs. The beginnerWeapon[] is type weapon and has elements 0 and 1 declared in another file:
void PopulateShop(location loc)
{
printf("DEBUG: character location pointer: %s\n\n\n", loc.name);
loc.shop.items[0] = beginnerWeapons[0];
loc.shop.items[1] = beginnerWeapons[1];
printf("DEBUG: loc.shop.items[0].name: %s\n\n\n", loc.shop.items[0].name);
}
void ShowItems(location loc)
{
printf("DEBUG: location pointer in ShowItems(): %s\n\n\n", loc.name);
printf("DEBUG: loc.shop.items[0].name: %s\n\n\n", loc.shop.items[0].name);
}
Now, when I pass a location to each function from another file through this function:...
void ShopMenu(location loc)
{
PopulateShop(loc);
ShowItems(loc);
}
...the debugs show the location name correctly in both functions, the correct weapon name shows up in PopulateShop(), but the shop.items[] array changes don't stick by the time I call ShowItems(). I'm not sure if I need to allocate memory for .items[], or externalize it, or pass pointers instead (which I'm not entirely sure how to do with subscripting), or perhaps I'm defining shop incorrectly. Any help is appreciated!