我确信这是显而易见的事情......但我现在已经多年了,并且无法发现我的方式的错误,所以希望一些新鲜的眼睛能指出它。
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只是一个典型的列表结构,就像节点一样。
正在发生的事情是每个节点都成功创建(其地址为0x7fd1dac03ab0, 0x7fd1dac03ac0等),但node-> next在每个节点上返回0x7fd1dac03960。就像我说的那样,我确信我在做一些非常明显的事情......但是我们将非常感激任何帮助。