I am working with linked list in C, but I think didn't understant very well the use of pointers.
I have a structure for my linked list. I initialize the first element to NULL. I send this pointer to a function to create (with malloc) a new element of the list. But after the call of the function my element is still NULL. I don't understand. It is surely a stupid error but I need some help..
typedef struct Goto Goto;
struct Goto
{
int index;
Goto *next;
};
//my code
Goto* gotoList = NULL;
addLabel(gotoList, index);
// Here : gotoList is NULL
void addLabel(Goto* gotoList, int value) {
if (gotoList == NULL) {
Goto* gotoLabel = malloc(sizeof(*gotoList));
gotoLabel->index = value;
gotoLabel->next = NULL;
gotoList = gotoLabel;
}
else {
Goto* gotoLabel = gotoList;
Goto* newLabel = malloc(sizeof(*newLabel));
newLabel->next = NULL;
newLabel->index = value;
while (gotoLabel->next != NULL) {
gotoLabel = gotoLabel->next;
}
gotoLabel->next = newLabel;
}
// Here : gotoList is not NULL
}
Thanks for helping me