C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-22-2008, 03:37 PM   #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 ;-)
G4B3 is offline   Reply With Quote
Old 07-22-2008, 03:47 PM   #2
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
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, etc.

New project: nort
dwks is offline   Reply With Quote
Old 07-23-2008, 03:54 AM   #3
Registered User
 
Join Date: Mar 2008
Posts: 71
yes, you're right, I didn't realize that :-) Thx for the help
G4B3 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Doubly linked lists mohanlon C Programming 7 06-27-2009 07:45 PM
Linked Lists 101 The Brain C++ Programming 5 07-24-2004 04:32 PM
Request for comments Prelude A Brief History of Cprogramming.com 15 01-02-2004 10:33 AM
eof in fstream & singly linked lists mazo C++ Programming 3 06-03-2002 09:50 AM
Linked lists and file i/o, and some other stuff ninja C++ Programming 9 05-19-2002 07:15 PM


All times are GMT -6. The time now is 01:50 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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