When and why to use multiple indirection ?

This is a discussion on When and why to use multiple indirection ? within the C Programming forums, part of the General Programming Boards category; Hi all, I'm new to C programming and I'm reading a book about C data structures to get more expertise ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    São Caetano do Sul
    Posts
    1

    When and why to use multiple indirection ?

    Hi all,

    I'm new to C programming and I'm reading a book about C data structures to get more expertise with the language. I'm a Java Programmer and I really can't figure out why multiple indirection is used

    Such the linked list example below.:

    Code:
    int list_rem_next(List *list, ListElmt *element, void **data);
    Why "data" is a pointer to pointer ? Wouldn't be a normal pointer *data enough ? Since a pointer is a reference to an address ...

    Thank you very much
    Marcos

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Why do pointers exist? So that you can change the value of a variable from another scope, for one thing. What if the variable you wanted to change was a pointer itself?
    Code:
    char name[] = "Anonymous";
    set_name(&name);
    
    void set_name(char **name) {
        strcpy(*name, "Samuel");
    }
    That's the main reason for pointers-to-pointers. If pointers to variables are a good thing, then sooner or later you're going to need a pointer to another pointer.

    void pointers are a special case. Because a void pointer can hold the value of any pointer, of any indirection level, this code is perfectly valid:
    Code:
    int x;
    void *a = &x, *b = &a, *c = &c;
    *(int *)a = 0;
    **(int **)b = 0;
    ***(int ***)c = 0;
    You just have to remember what you stored in the void pointer when you decide to go and retrieve it.

    There are issues with pointers-to-pointers, of course. const doesn't play well with them, for reasons I don't feel like explaining. Many people would rather return a modified pointer rather than take a pointer-to-pointer as a parameter to a function:
    Code:
    char name[] = "Anonymous";
    strcpy(name, set_name(name));
    
    char *set_name(char *name) {
        return "Samuel";
    }
    But pointers-to-pointers definitely have their place. If you do a board search you might turn up some more information.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21