What is the difference between struct node * head and struct node ** head?

I am trying to sort a linked list. I am confused about when to use struct node*head and when to use struct node **head , the implementation can be done using both of them.

When should I use:

 void sortedinsert(struct node **head) 

and when to use:

 void sortedinsert(struct node *head) 
+4
source share
10 answers

With this function signature:

 void changeNode(struct node *head) 

You have a pointer to a node, and you can change this structure. You cannot change what the variable head points to. Suppose the following definition of a struct node :

 struct node { int field1; struct node *next; } 

Given a function signature and a struct node consider the following operations that can change the structure in a function:

 void changeNode(struct node *head) { head->field1 = 7; head->next = malloc(sizeof(struct node)); } 

C is the value passed: when we pass a variable to a function, the function receives a copy. This is why we pass a pointer to a struct node so that we can change it, and the consequences of these changes are outside the function. But we still get only a copy of the pointer itself. Thus, the following operation is not useful:

 void changeNode(struct node *head) { // we're only changing the copy here head = malloc(sizeof(struct node)); } 

Changes to head will not be displayed outside the function. To change what head indicates, we must use an additional level of indirection:

 void changeNode(struct node **head) { // now we're changing head *head = malloc(sizeof(struct node)); // alternately, we could also do this: *head = NULL; } 

Now changes to the head are reflected outside the function.

+6
source

if the head should always point to the beginning of the list of links (permanent location), then use struct node * head If you plan to change the location indicated by the head, use node ** head

+2
source

struct node **head you pass the address of the head pointer there to make it a link / point to another memory area. With struct node *head you cannot change head to point elsewhere

+1
source

The first is a pointer to a node, which is a structure.

 (struct node *) head; 

defines head as a variable that can store the node address.

This allows you to pass the node by reference in the method.

The second is a pointer to a pointer to a node, which is a structure.

 (struct node **) head; 

defines head as a variable that can store the address of another variable that has a node address.

This allows you to pass node * by reference in a method.

0
source

node* is a pointer to a node structure. node** is a pointer to a pointer to a node structure. Pointers to pointers are used in C if you want to change a pointer by reference.

Suppose you want to perform an operation on node B that could replace node B with another node. One way to do this is

nodeA.next = foo(nodeA.next);

Another option is just to do

foo(&nodeA.next);

And foo implicitly replaces the nodeA.next pointer.

0
source

In this example, since the value of the head may change (it may point to some other nodes other than the current one) when it is created,

You have to use

 void sortedinsert(struct node **head) 

as a change in your head may be required.

And below the protopts should be changed, it seems

 void sortedinsert(struct node *head) 

since this does not allow you to change the head, it should be in this case (if you use this),

 struct node * sortedinsert(struct node *head) 

which returns the updated head, which can be used in the calling function.

0
source

Run or read it, you can see it

 #include <stdio.h> struct node{ int one; int two; struct node * next; //char location[100]; }; void changeHead(struct node** head){ } void sort(struct node* head){ } int main(){ struct node* head = (struct node*) (malloc (sizeof(struct node))); // now head pointing to a node stucture ( if you dereferance head you get teh first value) struct node* tmp = head; struct node** newHead = (struct node**) (malloc (sizeof(struct node*))); //New head points to a 'struct node*', which hold an addtess of another struct node head->one = 12;//*head->one =12; //head.one = 12 is wrong cos it is holding an address. // you can do it but it doesnt make sence since you dont know whats on address #12 // now if you want head to point to old head which is on tmp at the moment *newHead = head; // now if you working with 2 linked list and you want to change the heads use below changeHead(newHead); // if you want to just sort its better and easy to use sort(head); //chack whats on head and newhead printf("double derefence newHead:%d\n",**newHead); printf("derefence newHead(its and address same ad address of head):%d\n",*newHead); printf("Head(its and address):%d\n",head); printf("derefence Head:%d\n",*head);//head->one works too } 
0
source

In simple words, use **head when you want to change the location indicated by "head" else, use *head where it does not allow you to change the location.

0
source
  • To make changes to the head, which is indicated on the first node, you need to pass the address of the head, i.e. (&head) to call (therefore, you pointed to a pointer to a pointer variable in a function (struct node**head) , because it gets the address of head , which is nothing more than a pointer to the first node.
  • However, if you know that the head will remain fixed, and you do not want the head to point to some other node (for example, do not insert a new node at the beginning), then you can go through (head) to call the function and write (*head) in the function definition.
0
source

All Articles