Hi,
I have had a nagging doubt about the double pointers & I would appreciate some clarification. The following code is an e.g. of double pointers. It works on linked list & creates a copy of a linked list (q).
Code:
copy_linked_lists(struct node *q, struct node **s)
{
if(q!=NULL)
{
*s=malloc(sizeof(struct node));
(*s)->data=q->data;
(*s)->link=NULL;
copy_linked_list(q->link, &((*s)->link));
}
}
I would like to know why this code has a double pointer passed to it. Suppose I "twist" the code to make it single pointered what could potentially go wrong with it.
Code:
copy_linked_lists(struct node *q, struct node *s)
{
if(q!=NULL)
{
s=malloc(sizeof(struct node));
s->data=q->data;
s->link=NULL;
copy_linked_list(q->link, s->link);
}
}
My understanding is double pointers are used when you don't want the address you are working on being destroyed in stack (as in function exit). Am I right? If so why I would really appreciate if someone can explain to me why the double pointer is used in the above code.