Pointers to pointers in linked lists

This is a discussion on Pointers to pointers in linked lists within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm a beginner in C++ trying to write himself a class for a new datatype that wouldn't be ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    71

    Pointers to pointers in linked lists

    Hi all,

    I'm a beginner in C++ trying to write himself a class for a new datatype that wouldn't be as "inflexible" as the default ones concerning storage capacity. I'm using linked lists to store the value of the number, and I wrote this piece of code to add a node to the end of the list, but I have absolutely no idea if it'll work. In fact, knowing how my experiments usually end, I have a hunch it won't But just to be sure, could anyone go over the code and tell me if it'll work or not?

    Code:
    void large::createNode(storage_list** param) {
        storage_list *temp; //Create a temporary pointer
    
        temp = new storage_list; //Allocate memory
    
        temp->next_node = NULL; //Set pointer to next node
        temp->previous_node = *param; //Set pointer to previous node
    
        *param->next_node = temp; //Assign "value"
    
        lastNode = temp; //Set pointer defined in class large
    }

    large is the class, and storage_list is the linked list (actually it's a double-linked list, isn't it...?)

    Thanks in advance ;-)

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Just one thing: instead of this
    Code:
    *param->next_node = temp; //Assign "value"
    I think you want this.
    Code:
    (*param)->next_node = temp; //Assign "value"
    Operator precedence dictates that without the parentheses, your code is like this:
    Code:
    *(param->next_node) = temp; //Assign "value"
    And I'm pretty sure that's not what you want, especially since param->next_node should be NULL at this point unless you want a memory leak . . . .
    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.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    yes, you're right, I didn't realize that :-) Thx for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked lists
    By mohanlon in forum C Programming
    Replies: 8
    Last Post: 12-08-2010, 12:01 AM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. eof in fstream & singly linked lists
    By mazo in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2002, 09:50 AM
  5. Linked lists and file i/o, and some other stuff
    By ninja in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2002, 07:15 PM

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