I'm sure it's something obvious... but I've been at it for ages now and just can't spot the error of my ways, so hopefully some fresh eyes will nail it.
BOOLEAN init()
{
struct list *foodList = (struct list*)malloc(sizeof(struct list));
struct node *head = (struct node*)malloc(sizeof(struct node));
head->data = NULL;
head->next = NULL;
foodList->head = head;
if (NULL == foodList) {
printf("List creation failed");
return FALSE;
}
return TRUE;
}
void add_node(struct list *foods, struct food * newFood) {
struct node *curr = foods->head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = (struct node*)malloc(sizeof(struct node));
curr->next->data = newFood;
printf("%p\n", curr);
curr->next->next = NULL;
}
List is just a typical list struct, as is node.
What's happening is each node is getting created successfully (their addresses being 0x7fd1dac03ab0, 0x7fd1dac03ac0, etc.), but the node->next is returning 0x7fd1dac03960 on every node. Like I said, I'm sure I'm doing something painfully obvious here... But any help would be greatly appreciated.