First off I just want to point out that I'm fairly new to C. I've done a bit of playing around with pointers and have (mostly) successfully built a linked list that works fine. The issue I'm having now is I'm trying to divide up the initialization portion of a game I'm working on into multiple functions called from inside an Initialize() function. It's starting to get real ugly quickly as I add pointers to try and maintain changes so the values are actually initialized when I get past Initialize().
From my understanding to change the address that a pointer points to you need to pass in a pointer to that pointer. I've tried that and it works fine if I change said address after only going one function deep. But when I try to pass this to another function, I have no idea as to what I should do both to pass it as a parameter and use it inside the next function (should I or should I not dereference it, if so how many times, what syntax should I use for this, etc.).
Right now I'm getting a compile error:
Code:
mylib/game.c: In function `LoadLayers':
mylib/game.c:146: error: request for member `object' in something not a structure or union
I'm not sure if I'm just not dereferencing it right or what (I've tried just one *, using ->object, etc.). Help is much appreciated!
This is what I have so far (just out of the affected functions).
Code:
/* From right before my primary game loop */
/* --DATA DECLARATIONS-- */
GameCharacter *player;
LinkedList_AA *artBank;
LinkedList_GO *layers;
/* END DATA DECLARATIONS */
layers = NULL;
artBank = NULL;
player = NULL;
Initialize(&player, &artBank, &layers);
if (player == NULL)
{
printf("\nPLAYER IS NULL!");
}
if (artBank == NULL)
{
printf("\nARTBANK IS NULL!");
}
if (layers == NULL)
{
printf("\nLAYERS IS NULL!");
}
Code:
/* Initialization function definitions */
void Initialize(GameCharacter **player,
LinkedList_AA **artBank, LinkedList_GO **layers)
{
*artBank = InitializeList_LL_AA();
LoadArtBank(*artBank);
*player = LoadGameCharacter("assets/objectdata/player.txt",
*artBank, EmptyVector2D(), "player");
LoadLayers(layers, player);
printf("\n\nLoad successful!\n\n\n");
}
void LoadArtBank(LinkedList_AA *artBank)
{
ArtAsset *testAsset;
testAsset = LoadArtAsset("assets/art/test.txt");
AddNode_LL_AA(artBank, testAsset);
}
void LoadLayers(LinkedList_GO** layers, GameCharacter** player)
{
int numLayers;
numLayers = 3;
*layers = InitializeListArray_LL_GO(numLayers);
/* Right here is the line that's giving me grief
- no matter how I deference it I get the compile
error listed below */
AddNode_LL_GO(layers[2], **player.object);
}
Code:
/* From my linked list library */
LinkedList_GO* InitializeListArray_LL_GO(int numLists)
{
LinkedList_GO *newLists;
int i;
newLists = malloc(numLists * sizeof(LinkedList_GO));
for(i = 0; i < numLists; i++)
{
newLists[i].current = NULL;
newLists[i].start = NULL;
}
return newLists;
}
/* Function prototype - I don't believe the inner workings
are important for solving this issue */
bool AddNode_LL_GO(LinkedList_GO *list, GameObject *newData);
Code:
/* Game character definition */
typedef struct GameCharacter
{
Vector2D velocity;
/* This is what I'm trying to access */
GameObject object;
} GameCharacter;
To clarify a few things:
----------------------
- I want layers to be an array of linked lists - so I can call layers based on their index (i.e. layer[0] = layer 0)
- GO stands for GameObject, AA stands for ArtAsset (both structs I created and am using)