I have run this code through the debugger and i can see the "menu->headInventory" get populated with the data from the "new" node. But as soon as it leaves the function the data stored in the "menu->headInventory" is no longer accessible, Im passing by reference so why is this occurring?
Code:
if ((validation = addNode(&menu, &lineBuffer, &new)) != TRUE) {
fprintf(stderr, "Unable to add to node.\n");
exit(EXIT_FAILURE);
}
Code:
int addNode(HeadNode *menu, char *lineBuffer, InventoryTypePtr new) {
char *tokens = NULL, delims[] = "|", type;
int count = 1; /*case 1*/
tokens = strtok(lineBuffer, delims);
new = (InventoryTypePtr)malloc(sizeof(InventoryTypePtr));
if (new == NULL) {
fprintf(stderr, "Memory allocation for current failed");
return FALSE;
}
/* Populate the node with the imported information from the
line buffer */
while (tokens != NULL) {
printf("%s", tokens);
switch (count) {
case 1 :
strcpy(new->inventoryID, tokens);
break;
case 2 :
type = *tokens;
new->foodType = type;
break;
case 3 :
strcpy(new->inventoryName, tokens);
break;
case 4 :
strcpy(new->inventoryDescription, tokens);
break;
}
count++;
tokens = strtok( NULL, delims);
}
new->nextInventory = menu->headInventory;
menu->headInventory = new;
menu->numInventoryNodes += 1;
return TRUE;
}