Thread: Why I should be use double pointers?

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    21

    Why I should be use double pointers?

    I understood that when the function changes the value of pointer (and not the value witch the pointer points..), we should be use a double pointer. This because the function works on a "copy" of arguments passed, so, at the end, the function doesn't will change the original pointer's value..right?
    I'm always working on my Doubly Linked List, that until this moment I wrote using only single pointers, and all works!
    This is my structs (In italian lista = list):
    Code:
    typedef struct Node {
        void *data;
        struct Node *next;
        struct Node *prev;
    }Node;
     
    typedef Node* iterator;
    
    
    typedef struct Lista {
        Node *head;
        Node *tail;
        int length;
    }Lista;
    For example, I have this function Add() that insert a node at the end of list:
    Code:
    void Add(Lista *lista, void *data) {
        Node *n = New_Node(data);
        if (!lista->tail)
            lista->head = n;
        else {
            lista->tail->next = n;
            n->prev = lista->tail;
        }
        lista->tail = n;
        lista->length++;
    }
    I have seen more times this code written using double pointers..but why? In my code, lista->tail pointer remains changed..then, when I should use double pointers?!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,506
    You encapsulated the node into a list, so you already have an indirect "double pointer", i.e., the pointer to the list, with the list's member pointer pointing to the node.

    Try writing the function without the list struct:
    Code:
    void Add(Node *head, Node *tail, int length, void *data) {
        Node *n = New_Node(data);
        /* ... */
    }
    You will soon see that it has to be:
    Code:
    void Add(Node **head, Node **tail, int *length, void *data) {
        Node *n = New_Node(data);
        /* ... */
    }
    hence the advantage of having the list struct.
    Last edited by laserlight; 03-10-2018 at 05:54 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    21

    Post

    Quote Originally Posted by laserlight View Post
    You encapsulated the node into a list, so you already have an indirect "double pointer", i.e., the pointer to the list, with the list's member pointer pointing to the node.
    Ohhh I get it! Thank you!

    At this point, I would like to know for what purposes (or into what Abstract Data Type) double pointers are most used.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,736
    Read this:

    Linus on understanding pointers - Gregory Trubetskoy

    Especially the part where it says

    What Linus was saying is that the above code could be simplified by making the previous element a pointer to a pointer rather than just a pointer.
    and the code example given in that context

  5. #5
    Registered User
    Join Date
    Feb 2018
    Posts
    21
    Quote Originally Posted by Hodor View Post
    Read this:

    Linus on understanding pointers - Gregory Trubetskoy

    Especially the part where it says



    and the code example given in that context

    Did it! I will needed a lot of practice to understand completely this argument..

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,649
    Using a double pointer also simplifies the code for scanning a list where the initial pointer is the head pointer, and later pointers are node's next pointers, eliminating the need for a conditional to check if working with the head or a node:

    Code:
    /* initialize ptr to ptr to node */
        Node **ppnode = &head;
    /* advance ptr to ptr to node */
        while(*ppnode != NULL){
            ppnode = &((*ppnode)->next);
    /* ... */
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2015, 01:15 PM
  2. pointers to double
    By Satya in forum C Programming
    Replies: 2
    Last Post: 07-10-2015, 09:48 AM
  3. Double pointers
    By Anator in forum C Programming
    Replies: 1
    Last Post: 10-10-2009, 07:06 AM
  4. double pointers?
    By Kohatian 3279 in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2002, 04:16 AM
  5. more help on double pointers
    By drharv in forum C Programming
    Replies: 6
    Last Post: 03-01-2002, 10:31 AM

Tags for this Thread