Originally Posted by
mrdiegus
I was trying to do some linked lists exercises, and I'd like to ask you what is the difference between the following two prototypes?
Code:
void insert(ListNodePtr sPtr, char value);
void insert(ListNodePtr *sPtr, char value);
Is one of this wrong? The meaning is the same or the second one means that sPtr is a pointer to a second pointer?
There is a big difference.
The first version takes a pointer to ListNode. If you change the pointer inside the function (so it points to somewhere else, i.e. sPtr = xxxx) the change won't be visible to the caller. For example if you have this code:
Code:
ListNodePtr head = NULL;
printf("%p\n", (void *) head);
insert(head, 'a');
printf("%p\n", (void *) head);
"head" will still point to NULL after calling the function no matter how you change "sPtr" within insert().
In your second version, you pass a pointer to pointer to ListNode, thus changes to the pointer to ListNode (*sPtr = xxxx) will be visible to the caller.
You wouldn't say
Code:
void func(int value);
void func(int *value);
are the same, would you? It's the same when you use pointers in function calls. Arguments are always passed by value in C.
Bye, Andreas