akash
akash

Reputation: 1801

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

I am trying to sort a linked list. I'm 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 should I use:

void sortedinsert(struct node *head)

Upvotes: 7

Views: 38084

Answers (10)

Syed Waris
Syed Waris

Reputation: 1074

Case 1: When you are using:

void sortedinsert(struct node **head)

Inside the function you will ACTUALLY be modifying the head pointer, since most probably you will be using *head inside. The sortedinsert will most probably be called with the argument &head.

Case 2: When you are using:

void sortedinsert(struct node *head)

Inside the function you will be modifying the copy of the head pointer, since most probably you will be using head variable inside. The sortedinsert will most probably be called with the argument head.

Upvotes: 1

user3847870
user3847870

Reputation: 410

  1. To make the change to the head which is pointer to the first node,you need to pass address of head ,i.e (&head) to call the function(therefore you have declared pointer to the pointer variable in the function (struct node**head) as it receives address of head which is nothing but pointer to the first node.
  2. However if you know that head will remain fixed i.e you don't want head to point to some other node(like..no inserting a new node at the beginning) then you can only pass (head) to call the function and write (*head) in the function definition.

Upvotes: 0

Developer
Developer

Reputation: 21

In simple words use your **head when you want to change the location pointed by "head" else use *head where it doesn't allow to change the location.

Upvotes: 0

printfmyname
printfmyname

Reputation: 971

Run or read this 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
}

Upvotes: 0

pb2q
pb2q

Reputation: 59607

With this function signature:

void changeNode(struct node *head)

You have a pointer to the node, and so you can change that structure. You can't change what the variable head points to. Let's assume the following definition of struct node:

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

With the given function signature and struct node, consider the following operations can change the structure in the function:

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

C is pass-by-value: when we pass a variable to a function, the function gets a copy. This is why we pass a pointer to a struct node, so that we can change it, and have the effects of those changes outside the function. But we still get only a copy of the pointer itself. So the following operation isn't useful:

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

The changes to head won't be reflected outside the function. In order to change what head points to, 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 the changes to head are reflected outside the function.

Upvotes: 15

Tanmoy Bandyopadhyay
Tanmoy Bandyopadhyay

Reputation: 985

In this example, since the value of head may change(It can point to some other nodes different from the current one) while it is being created,

You should be using,

void sortedinsert(struct node **head)

since your head may need to be modified.

And the below protoptye needs to be changed, it seems,

void sortedinsert(struct node *head)

since this does not allow you to modify head, It should be in this case (if you are using this),

struct node * sortedinsert(struct node *head)

which returns back the updated head, which can be used in the caller of this function.

Upvotes: 0

Cratylus
Cratylus

Reputation: 54074

struct node **head you are passing the address of the pointer of head there by making it possible to make it refer/point to a different memory area. With struct node *head you can't modify head to point anywhere else

Upvotes: 3

rshetye
rshetye

Reputation: 667

if head has always to point at the head of the link list(a constant location) then use struct node* head If you plan to change the location pointed by head use node **head

Upvotes: 2

Antimony
Antimony

Reputation: 39451

node* is a pointer to a node struct. node** is a pointer to a pointer to a node struct. Pointers to pointers are used in C when you want to modify a pointer by reference.

Say you want to do an operation on node B which could potentially replace node B with a different node. One way to do things is

nodeA.next = foo(nodeA.next);

The other option is to just do

foo(&nodeA.next);

And have foo implicitly replace the nodeA.next pointer.

Upvotes: 0

Bharat Sinha
Bharat Sinha

Reputation: 14363

First one is a pointer to node which is a structure.

(struct node *) head;

defines head as a variable which can store the address of a node.

This allows to pass node by reference in a method.

Second one is a pointer to a pointer to node which is a structure.

(struct node **) head;

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

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

Upvotes: 2

Related Questions