Reputation: 309
I am trying some operations on linked lists in C language. I am a newbie and sort of getting confused in the function append()
.
They have passed arguments in the function like struct node **q
. And they are using it with *q
.
Function append()
in C language:
void append(struct node **q, int num) {
struct node *temp, *r;
if(*q==NULL) {
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=NULL;
*q=temp;
}
else {
temp=*q;
while(temp->link!=NULL)
temp=temp->link;
r=malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;
}
}
I am not able to understand:-
**q
in arguments and *q
in the code part? Any help would be appreciated.
Upvotes: 1
Views: 632
Reputation: 1263
You have to understand the concept of pointers well. Generally if you work on pointers, you can access the value inside that pointer using * operator.
ex:
x=10; //Assume Memory Address of x is 1000. 1000 holds value 10
int *y=&x; //Assume Memory address of y is 1100. 1100(y) holds pointer value 1000
Here,
y will hold 1000
*y => value at y => value at 1000 => 10
Let the structure looks like this.
struct node
{
int data;
struct node *link;
}
Initially your queue is empty.
//Initially queue is empty.
struct node *queue=NULL;
append(&queue,10); //append(4000,10);
Here assuming that the address of queue is 4000.
4000 => queue => holds value 0
so here 4000 holds the start of the queue. Since the queue is empty it holds 0.
Here we have to access the value of 4000 to get the first node address. So you are going to access a pointer(4000) that points to a pointer(currently 0). In this case you are dealing with double pointers so your declarations is **q.
void append(struct node **q, int num)
Here q will be 4000, *q => value at q is NULL(Zero). Gets into if block.
if(*q==NULL) //*q => value at q is NULL(Zero)
{
temp=malloc(sizeof(struct node)); //assume temp=3000
temp->data=num; //3000->data=10
temp->link=NULL; //3000->link=NULL;
*q=temp; //*q => value at q => value at 4000 is 3000 now.
}
Now calling append again
append(&queue,20); //append(4000,20);
4000 => queue => holds value 3000 //updated address in queue
If the list is not empty, it will work on else block.
temp=*q; // temp= value at q => 3000
//Iterate from 3000 to traverse the list and add num in the end.
Upvotes: 0
Reputation: 409166
Remember that arguments in C are passed by value, meaning that their values are copied. So to change an argument in a function, you have to pass it by reference. In C this is done by using pointers. However, a pointer itself when passed to a function is also passed by value, so to be able to change that pointer you have to pass it by reference, hence you pass it as a pointer to the pointer.
For your specific code, the function append
modified the pointer you pass top it, and so you need to pass it by reference with the address of the pointer. The caller does something like:
struct node *queue;
append(&queue, ...);
Then when append
returns, q
may be changed.
The usage of *q
in the function is because the unary *
operator is for dereferencing a pointer. So if you have a pointer q
then *q
will be the value that q
points to. In the case of your function, since q
is a pointer to a pointer then *q
will result in the original pointer.
If called using my short snippet above, then *q
will be returning the queue
pointer.
Upvotes: 3
Reputation: 9474
This is designed to pass the address of head pointer to append()
.
Im pretty sure, append will be called like this,
int main()
{
struct node *k;
//calling append() function.
append(&k);
}
Refer Here for some simple example. http://www.cplusplus.com/forum/general/13227/
Upvotes: 0
Reputation: 993
**q means q is a pointer which points to another pointer say p. If i want to get the value of p using q i have to use like **q. *q will point the value in q which will be the address of p.Again a * will get the value of p.
Upvotes: 0