How to pass a pointer by reference? C++

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:
 
node *example_ptr=NULL;

Thanks.
Last edited on
You can simply pass the pointer by reference to a function.
1
2
node *ptr;
passbyref(ptr);

And change it with the following code.
(remember, read pointers right to left. Here, ptrRef is a reference to a pointer to a node type).
1
2
3
4
void passbyref (node *&ptrRef)
{
    ptrRef= 0;
}


Or simply pass a pointer to a pointer.
1
2
node *ptr;
passbyref(&ptr);

Then dereference that to access the previously declared pointer directly using a pointer to that pointer.
1
2
3
4
void passbyref(node **ptrptr)
{
    *ptrptr= 0;
}


Both do the same thing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

void Increment( int*& toGrow )
{
  ++toGrow;
}

int main( void )
{
  int i = 0;
  int* a = &i; // pointer to pass

  // print before increment
  std::cout << a << std::endl;

  Increment( a );

  // print after increment
  std::cout << a;

  return 0;
}


This is the simplest example I can think of. Below is a little twist with typedef. If you need to create complicated type(so bad thing to do though), typedef will help you.

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

typedef int*(*AnyType)[10][20];

void Increment( AnyType& a )
{
  ++a;
}

int main( void )
{
  AnyType a = 0;

  std::cout << a << std::endl;

  Increment( a );

  std::cout << a;

  return 0;
}
Last edited on
Thanks for your replies. I wrote a short pass by reference example in my existing code to test it and it worked fine. Now I need to figure out why I was having issues last night. My guess is it was my syntax. I will reply nonetheless and make sure to mark this as solved when I figure out the issues in my own code.
I don't remember exactly what I was doing last night, but when I tried to pass by reference, it would say the pointer is not initialized.

I'm not getting that error anymore after following Nexius' example:

You can simply pass the pointer by reference to a function.

1
2
node *ptr;
passbyref(ptr);


And change it with the following code.
(remember, read pointers right to left. Here, ptrRef is a reference to a pointer to a node type).

1
2
3
4
void passbyref (node *&ptrRef)
{
    ptrRef= 0;
}



Still making sure everything is working fine...
Everything is working fine now. Thanks to kg1992 and Nexius for both answering my question.

To anyone else with the same issue, make sure your syntax is like how Nexius explained above. My error was just with the syntax.
Issue solved. :)
Topic archived. No new replies allowed.