Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6476193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:47:40+00:00 2026-05-25T06:47:40+00:00

The two code examples below both add a node at the top of a

  • 0

The two code examples below both add a node at the top of a linked list.
But whereas the first code example uses a double pointer the second code example uses a single pointer

code example 1:

struct node* push(struct node **head, int data)
{
        struct node* newnode = malloc(sizeof(struct node));
        newnode->data = data;
        newnode->next = *head;
        return newnode;
}

push(&head,1);

code example 2:

struct node* push(struct node *head, int data)
{
        struct node* newnode = malloc(sizeof(struct node));
        newnode->data = data;
        newnode->next = head;
        return newnode;
}

push(head,1)

Both strategies work. However, a lot of programs that use a linked list use a double pointer to add a new node. I know what a double pointer is. But if a single pointer would be sufficient to add a new node why do a lot of implementations rely on double pointers?

Is there any case in which a single pointer does not work so we need to go for a double pointer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:47:41+00:00Added an answer on May 25, 2026 at 6:47 am

    Some implementations pass a pointer to pointer parameter to allow changing the head pointer directly instead of returning the new one. Thus you could write:

    // note that there's no return value: it's not needed
    void push(struct node** head, int data)
    {
        struct node* newnode = malloc(sizeof(struct node));
        newnode->data=data;
        newnode->next=*head;
        *head = newnode; // *head stores the newnode in the head
    }
    
    // and call like this:
    push(&head,1);
    

    The implementation that doesn’t take a pointer to the head pointer must return the new head, and the caller is responsible for updating it itself:

    struct node* push(struct node* head, int data)
    {
        struct node* newnode = malloc(sizeof(struct node));
        newnode->data=data;
        newnode->next=head;
        return newnode;
    }
    
    // note the assignment of the result to the head pointer
    head = push(head,1);
    

    If you don’t do this assignment when calling this function, you will be leaking the nodes you allocate with malloc, and the head pointer will always point to the same node.

    The advantage should be clear now: with the second, if the caller forgets to assign the returned node to the head pointer, bad things will happen.

    Edit:

    Pointer to pointer(Double pointers) also allows for creation for multiple user defined data types within a same program(Example: Creating 2 linked lists)

    To avoid complexity of double pointers we can always utilize structure(which works as an internal pointer).

    You can define a list in the following way:

    typedef struct list {
        struct node* root;    
    } List;
    
    List* create() {
        List* templ = malloc(sizeof(List));
        templ->root = NULL;
        return templ;
    }
    

    In link list functions use the above List in following way: (Example for Push function)

    void Push(List* l, int x) {         
        struct node* n = malloc(sizeof(struct node));
        n->data = x;
        n->link = NULL;
        
        printf("Node created with value %d\n", n->data);
        if (l->root == NULL) {
            l->root = n;
        } else {
            struct node* i = l->root;
            while (i->link != NULL){
                i = i->link;
            }
            i->link = n;
        }
    }
    

    In your main() function declare the list in follow way:

    List* list1 = create(); 
    push(list1, 10);
    
          
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two code examples below. One works, but the second (using extend to
My problem is deleting a node from linked list. I have two structs :
The code below plots some simple x-y data, but it has two problems that
I'm using doxygen for commenting code. There is, for example, two files: test1.cpp :
What's the difference between the two code below. int a[] = {0,0}; int a[2]
Update: I have provided complete code example in answer below. I have built my
The example below may not be problematic as is, but it should be enough
Below I have two tables users and users_profiles Both are innoDB and collation: utf8_general_ci
In my example code below, you can see that I have been trying to
The example below illustrates a more complex but not dissimilar problem I've been trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.