0

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

iAmoric
  • 1,787
  • 3
  • 31
  • 64
  • 1
    Calling a type `Goto` is not the best idea. – Eugene Sh. Dec 06 '17 at 17:36
  • Your problem is rooted in the fact that everything in `c` is pass by value. A local copy of `gotoList` is made in `addLabel` when you call that function, and its scope exists only in that function. If you want changes to `gotoList` to persist outside of `addLabel`, you must return `gotoList` or pass in a pointer to it (a `Goto**` type), and dereference that pointer in the function. – yano Dec 06 '17 at 17:40

1 Answers1

1

Well to make change either you have to pass the address of the variable from the caller function or return from the callee function the address of the allocated memory and assign it to respective variable.

Here you do neither of it so you don't get to retain the change.

By passing the adress of the variable you can do it like this:(The other can be done easily if you understand this one fully).

void addLabel(Goto** gotoList, int value) {
    if (*gotoList == NULL) {
        Goto* gotoLabel = malloc(sizeof(*gotoLabel ));
        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 what we did is simply passed the address of the variable. You will call the function like this

addLabel(&listhead,val);

One thing, you have a bad choice in selecting the names of the variables. Goto is the last choice of variable name. In C goto is a keyword, naming a variable on some variation of it is not only misleading but also erroneous in meaning too.

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • Thanks a lot ! Due to the fact that gotolist was a pointer, I thought that I could change the value, but I effectively need a pointer to a pointer. And I didn't know that Goto was a C keyword.. I will change – iAmoric Dec 06 '17 at 17:47
  • @iAmoric.: Pointer doesn't mean you can change it's value. It;s just an address, so you can change anything to the content of that address nothing else. – user2736738 Dec 06 '17 at 17:48
  • Yes that's what I meat, use the pointer to change the content at the address. Anyway, thanks for helping me. Question is solved – iAmoric Dec 06 '17 at 17:55