I have a struct for a Double List with next and prev pointers. I'm trying to create a second list where I'm putting the fruits with odd number of calories from the first list. But I don't know why, when the second list enters in the append function, after I set the first and last pointers to NULL, only the first pointer is still set on NULL, the last pointer being uninitialised.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct Node Node;
typedef struct DoubleList DoubleList;
typedef struct Fruit Fruit;
struct Fruit {
char* name;
int calories;
};
struct Node {
Fruit info;
Node* next, * prev;
};
struct DoubleList {
Node* first;
Node* last;
};
void append(DoubleList* list, Fruit f) {
Node* new = (Node*)malloc(sizeof(Node));
new->info = f;
new->next = NULL;
new->prev = NULL;
if (list->last) {
list->last->next = new;
list->last = new;
}
else {
list->last = list->first = new;
}
}
void main() {
DoubleList ld;
ld.first = NULL;
ld.last = NULL;
append(&ld, createFruit("Avocado", 201));
append(&ld, createFruit("Apple", 251));
append(&ld, createFruit("Strawberries", 100));
append(&ld, createFruit("Cherry", 50));
DoubleList* ld2 = (DoubleList*)malloc(sizeof(DoubleList));
//DoubleList ld2;
ld2->first = NULL;
ld2->last = NULL;
Node* temp = ld.first;
while (temp)
{
Node* aux = temp->next;
if (temp->info.calories % 2 == 1) {
append(&ld2, temp->info);
//but I don't know why this line won't send the list to the append function with the pointers set on NULL
//append(ld2, temp->info); this line is working
}
else {
free(temp->info.name);
free(temp);
}
temp = aux;
}
show(*ld2);
}


ld2is a pointer, so&ld2creates a pointer to a pointer. Not whatappendexpects. (Also, this is definitely C and not valid as C++).