I have an add function and use a realloc to take in the data from a struct and store it. But when I print it out, it says that it's empty, which comes from my display function. How can I fix this?
void additem(food *head, int index) {
head = (food*)realloc(head, sizeof(food)*(index + 1));
if (head != NULL) {
printf("Enter name: ");
gets((head + index)->name);
printf("Type unit of meauserement: ");
gets((head + index)->unit);
printf("Enter value: ");
scanf("%d", &(head + index)->data);
}
else {
printf("\nExiting!!");
free(head);
}
}
My main function looks like this:
food *foods = NULL;
int option, nofood = 0;
while (1) {
system("CLS");
printf("1 - add food\n2 - print shopping list\n3 - delete item\n4 - change amount\n5 - end\nWhat do you want to do? ");
scanf("%d", &option);
clear();
switch (option) {
case 1: additem(foods, nofood);
break;