Hey Forum,
I have read countless threads from others with the same question as mine...yet nobody actually answers it. They all seem to go in the direction of "Why are you using pointers by reference? You don't need to do that..."
Anyways, I am writing a program to create and sort a linked list full of nodes whose data fields are randomly generated. Each node has a data field with said random number, and a link field which is just a pointer to the next node. Eventually, I am going to have to change where each nodes' link field (pointer) points to.
I cannot figure out how to use a pointer by reference so that where it points can be changed. My book does not give an explicit example, which I think I need in this situation. I've never been so stuck before. Here's what my book gives as an example:
1 2 3
|
void list_head insert (node*& head_ptr, const node::value_type& entry);
// Precondition: head_ptr is the head pointer of a linked list.
// Postcondition: A new node containing the given entry has been added at the head of the list; head_ptr now points to the head of the new, longer linked list.
|
And later on, the statement to insert a node at the head of the linked list:
|
head_ptr = new node(entry, head_ptr)
|
Do I declare a pointer like normal if I want to change what it points to later? Can you provide an example of passing by reference?
EDIT: By "like normal", I mean:
Thanks.