0

I am now a python programmer and I started my carrier with C. After two years I thought to study data structures in C again.

Forgive me asking such a silly basic question, but I can't help thinking the fact that can't we mutate a pointer holds a NULL value, by passing it to a function?

void insertn(node* noo, int i){

    node* no=malloc(sizeof(node));
    no->i=i;
    no->next=noo;
    noo=no;
}

int main()
{
    node* o=NULL;
    insertn(o,10);
    printf("%d\n",o->i);
    return 0;
}

The problem in this code is that o's value is always NULL, it doesn't change in the function.

Why is that, can't we change a NULL pointer in another function?

Clarke
  • 185
  • 10
  • 3
    To do that you'd have to pass a pointer to that pointer to the function (it doesn't matter that the pointer is NULL by the way) – Felix G May 11 '20 at 11:36
  • @FelixG What happens in my one? – Clarke May 11 '20 at 11:38
  • 2
    In your code, you only modify your local copy of that pointer (resulting in a memory leak, because `no` will just be forgotten when returning from `insertn`). What you could do to achieve the desired effect is define the function like this: `void insertn(node** noo, int i) { ... no->next = *noo; *noo = no;}` (obviously replace ... with your malloc call and initialization of `no`) and call it like this: `insertn(&o, 10);` – Felix G May 11 '20 at 11:42
  • @FelixG Ok now I get it. Thank you very much. – Clarke May 11 '20 at 11:43
  • 2
    @Clarke `o` and `noo` are different pointers. You pass the value of `o`, actually `NULL` by value to `noo` and modify only `noo`. Furthermore, After the execution of `insertn()` the local pointer `noo` does not exist anymore. – RobertS supports Monica Cellio May 11 '20 at 11:44
  • @RobertSsupportsMonicaCellio Oh then since `o` holds no memory address, but the value `NULL`, the value that get copied to `noo` is `NULL`. So when we assign another memory address for `noo`, it is just a local variable in the function. Then it is discarded after function exists. Am I correct? – Clarke May 11 '20 at 11:50
  • 1
    @Clarke Yes, but the allocated memory by `malloc()` isn´t freed/deallocated with that, just the pointer itself. Usual way is to define `ptr` in `main()`, pass a pointer to this pointer (`**`)(as Felix already said) to the function and then dereference the pointer to pointer to allocate the memory. Note, always proof `ptr` for `NULL` after the function call if the allocation succeeded or the allocation failed. – RobertS supports Monica Cellio May 11 '20 at 11:57
  • @RobertSsupportsMonicaCellio Yeah thank you very much now I see the mistake I made! – Clarke May 11 '20 at 12:02
  • 2
    It might also help to think about this in a more abstract way: if you want to use a function to modify an object, the function needs a pointer to that object. In your case, the object you want to modify just happens to be a pointer, but that doesn't matter at all (you want to modify `o`, not what `o` is pointing to, so therefore you need a pointer to `o` to do that). – Felix G May 11 '20 at 12:14
  • @FelixG Yes now I remember it. Thanks for the advice – Clarke May 11 '20 at 13:08

0 Answers0