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

30,050

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
30,050
akash
Author by

akash

Open Source Projects 1- https://github.com/akash2504/twitter-oauth

Updated on July 05, 2022

Comments

  • akash
    akash almost 2 years

    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)