Passing Reference to a Pointer in C++

Prerequisite: Pointers vs References in C++.

For clear understanding, let’s compare the usage of a “pointer to pointer” VS “Reference to pointer” in some cases.

Note: It is allowed to use “pointer to pointer” in both C and C++, but we can use “Reference to pointer” only in C++.



Passing pointer to a function

If a pointer is passed to a function as a parameter and tried to be modified then the changes made to the pointer does not reflects back outside that function. This is because only a copy of the pointer is passed to the function. It can be said that “pass by pointer” is passing a pointer by value. In most cases, this does not present a problem. But the problem comes when you modify the pointer inside the function. Instead of modifying the variable, you are only modifying a copy of the pointer and the original pointer remains unmodified.

Below program illustrate this:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
  
using namespace std;
  
int global_Var = 42;
  
// function to change pointer value
void changePointerValue(int* pp)
{
    pp = &global_Var;
}
  
int main()
{
    int var = 23;
    int* ptr_to_var = &var;
  
    cout << "Passing Pointer to function:" << endl;
  
    cout << "Before :" << *ptr_to_var << endl; // display 23
  
    changePointerValue(ptr_to_var);
  
    cout << "After :" << *ptr_to_var << endl; // display 23
  
    return 0;
}

chevron_right


Output:

Passing Pointer to function:
Before :23
After :23

Passing “pointer to a pointer” as a parameter to function

The above problem can be resolved by passing the address of the pointer to the function instead of a copy of the actual function. For this, the function parameter should accept a “pointer to pointer” as shown in the below program:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
  
using namespace std;
  
int global_var = 42;
  
// function to change pointer to pointer value
void changePointerValue(int** ptr_ptr)
{
    *ptr_ptr = &global_var;
}
  
int main()
{
    int var = 23;
    int* pointer_to_var = &var;
  
    cout << "Passing a pointer to a pointer to function " << endl;
  
    cout << "Before :" << *pointer_to_var << endl; // display 23
  
    changePointerValue(&pointer_to_var);
  
    cout << "After :" << *pointer_to_var << endl; // display 42
  
    return 0;
}

chevron_right


Output:

Passing a pointer to a pointer to function 
Before :23
After :42

How to call a function with “Reference to pointer” parameter?

A reference allows called function to modify a local variable of the caller function. For example, consider the following example program where fun() is able to modify local variable x of main().

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<iostream>
  
using namespace std;
  
void fun(int &x) { 
    x = 20; 
    
int main() { 
    int x = 10; 
    fun(x); 
    cout<<"New value of x is "<<x; 
    return 0; 
}

chevron_right


Output:


New value of x is 20

Below program shows how to pass a “Reference to a pointer” to a function:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
  
using namespace std;
  
int gobal_var = 42;
  
// function to change Reference to pointer value
void changeReferenceValue(int*& pp)
{
    pp = &gobal_var;
}
  
int main()
{
    int var = 23;
    int* ptr_to_var = &var;
  
    cout << "Passing a Reference to a pointer to function" << endl;
  
    cout << "Before :" << *ptr_to_var << endl; // display 23
  
    changeReferenceValue(ptr_to_var);
  
    cout << "After :" << *ptr_to_var << endl; // display 42
  
    return 0;
}

chevron_right


Output:

Passing a Reference to a pointer to function
Before :23
After :42

Returning a pointer from a function

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
  
using namespace std;
  
int global_var = 42;
  
// function to return a pointer
int* returnPointerValue()
{
    return &global_var;
}
  
int main()
{
    int var = 23;
    int* ptr_to_var = &var;
  
    cout << "Return a pointer from a function " << endl;
  
    cout << "Before :" << *ptr_to_var << endl; // display 23
  
    ptr_to_var = returnPointerValue();
  
    cout << "After :" << *ptr_to_var << endl; // display 42
  
    return 0;
}

chevron_right


Output:

Return a pointer from a function 
Before :23
After :42

Returning reference from function

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
  
using namespace std;
  
int global_var = 42;
  
// function to return reference value
int& ReturnReference()
{
    return global_var;
}
  
int main()
{
    int var = 23;
    int* ptr_to_var = &var;
  
    cout << "Returing a Reference " << endl;
  
    cout << "Before :" << *ptr_to_var << endl; // display 23
  
    ptr_to_var = &ReturnReference();
  
    cout << "After :" << *ptr_to_var << endl; // display 42
  
    return 0;
}

chevron_right


Output:

Returing a Reference 
Before :23
After :42


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai