2

I have a question about passing via pointers in C. I will illustrate my question through this code snippet that I wrote:

int main () {
    List *head = (List *)malloc(sizeof(List));
    head->data = 0;
    head->next = NULL;
    insertEnd(head, 3);
    //List *temp = head;
    insertEnd(head, 4);
    PrintList(temp);
    return 0;
}

void insertEnd(List *node, int elem) {
    while (node->next != NULL) {
        node = node->next;
    }
    List *new_node = (List *)malloc(sizeof(List));
    new_node->data = elem;
    new_node->next = NULL;
    node->next = new_node;
}

void PrintList(List *node) {
    while (node) {
        printf ("%i ->", node->data);
        node = node->next;
    }
}

In the above code snippet, I create a List pointer called head, and pass it into my insertEnd() function two times. If you notice in my insertEnd() function, I am actually moving this passed in pointer along by doing a "node->next"

If I am changing the address this pointer points to within my insertEnd() function, then how come when I call PrintList() its as if the head pointers location never moved!

PrintList() returns: "0 -> 3 -> 4 ->"

Is this because when passing by pointer, you are actually passing a new physical copy of the pointer into the function, similar to passing by value, except in this case instead of copying over the entire value/data structure you simply copy over the pointer variable? I hope this makes sense..

Deidrei
  • 2,125
  • 1
  • 14
  • 14
AyBayBay
  • 1,726
  • 4
  • 18
  • 37
  • 1
    Your explanation seems correct to me. You are passing the value of the pointer as a parameter, which allows you to change the data referenced by the pointer, but not the pointer itself. – kuroi neko Jan 09 '14 at 08:09
  • Yeah thats what I thought but messing with C has got me second guessing my fundamentals. I mostly code in much higher level languages so its new to me – AyBayBay Jan 09 '14 at 08:10
  • In addition: [do not cast the result of malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – pzaenger Jan 09 '14 at 08:16
  • @pzaenger why should I not do that? – AyBayBay Jan 09 '14 at 08:19
  • http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – AndersK Jan 09 '14 at 08:25
  • @AyBayBay: Just check the link and read the accepted answer. – pzaenger Jan 09 '14 at 08:27
  • BTW normally it is better style not to use the argument to a function as a local variable and instead keep the original for reference. otherwise if the function gets more complex it may get a bit difficult to read. – AndersK Jan 09 '14 at 08:28
  • Please, please, _please_, ***please***: do not cast the `void *` returned by `malloc` when writing C – Elias Van Ootegem Jan 09 '14 at 08:31

1 Answers1

2

Yes, when you pass a pointer in a function argument the value of the address is copied so if you do :

void foo(int *f) {
    f = malloc(sizeof(int));
    printf("%d\n", f); 
}

void goo() {
   int *p = malloc(sizeof(int));
   foo(p);
   printf("%d\n", p);
}

they will print different results. This goes the same in your example when you pass head to the functions its address is copied to the node function argument. Changes to the values of node will not affect the value head (Only when you change the value pointed by these variables then if they point to the same address both *head and *node will show the same).

giorashc
  • 13,691
  • 3
  • 35
  • 71