Functionality and Difference Between *& and **& in C++
-
Functionality and Difference Between
*&
and**&
in C++ - Handle Values by Pointer in C++
- Handle Values by Reference Pointer in C++
- Handle Values by Pointer to Pointer Reference (Double Pointer) in C++

This guide explains the difference and the functionality of *&
and **&
in C++. To understand these symbols, you need to be familiar with the concept of pointers in C++.
So, before you go ahead and read the whole guide, visit the guide about pointers. Once you have understood this concept, it’ll be easy to understand this guide.
Functionality and Difference Between *&
and **&
in C++
When we look to handle values by pointer, we use a pointer. Its syntax is *ptr
.
Let’s understand this whole concept with the help of the following examples.
#include <iostream>
using namespace std;
// global variables
int value_1 = 10, value_2 = 20, *global_ptr;
We are simply creating two variables, value_1
, value_2
, and one global pointer as *global_ptr
. Next, we have made three different functions and methods to clarify everything.
Those functions are as follows.
Handle Values by Pointer in C++
// function which handles values by pointer
void byPointer(int *ptr) {
*ptr = 5;
ptr = global_ptr;
*ptr = 3;
}
The first function handles values by the pointer, as you can see that its parameter is a pointer.
Inside, we are simply overriding the pointer’s value, pointing it to a global pointer and later overriding its value again. Let’s see the main()
function.
int main() {
// a pointer to value 1
int *ptrValue_1 = &value_1;
// a global pointer having an address of value 2
global_ptr = &value_2;
// values before calling function
cout << "Value_1 = " << value_1 << endl;
cout << "Value_2 = " << value_2 << endl;
cout << "ptrValue_1 = " << ptrValue_1 << endl;
cout << "global_ptr = " << global_ptr << endl;
byPointer(ptrValue_1);
// values after calling function
cout << "Value_1 = " << value_1 << endl;
cout << "Value_2 = " << value_2 << endl;
cout << "ptrValue_1 = " << ptrValue_1 << endl;
cout << "global_ptr = " << global_ptr << endl;
return 0;
}
First, we created a pointer that points to the address of the first variable. The global pointer is pointing to the address of the second variable.
We are simply passing the pointer ptrValue_1
to the function mentioned above, as you can see that the initial values of variables value_1
and value_2
are 10 and 20, respectively.
But inside the function byPointer()
, those values are overridden. The pointer is also pointing to the address of the global pointer, which is pointing to the address of value_2
; that’s why both values were changed.
The output of the above code is:
Value_1 = 10
Value_2 = 20
ptrValue_1 = 0x4b8000
global_ptr = 0x4b8004
Value_1 = 5
Value_2 = 3
ptrValue_1 = 0x4b8000
global_ptr = 0x4b8004
As you can see, both variables’ values are changed, but they did not change the address of the pointers. This is because it makes copies of pointers and handles values using the copies in this way of handling values.
That’s why the address remains unchanged. This concept will help you understand further.
Handle Values by Reference Pointer in C++
Now, let’s understand the concept of *&
in c++. Take a look at the following code.
// function which handle values by pointer Reference
void byPointerReference(int*& ptr) {
*ptr = 40;
ptr = global_ptr;
*ptr = 50;
}
In the above method, we pass a reference to a pointer as its parameter. It’ll work the same as the function in which we passed the pointer.
The only difference will be that it will also change the address of the pointers. After the method, the address of the pointers will be the same as the address of the global pointer according to this code.
int main() {
// a pointer to value 1
int *ptrValue_1 = &value_1;
// a global pointer having the address of value 2
global_ptr = &value_2;
// values before calling function
cout << "Value_1 = " << value_1 << endl;
cout << "Value_2 = " << value_2 << endl;
cout << "ptrValue_1 = " << ptrValue_1 << endl;
cout << "global_ptr = " << global_ptr << endl;
byPointerReference(ptrValue_1);
// values after calling function
cout << "Value_1 = " << value_1 << endl;
cout << "Value_2 = " << value_2 << endl;
cout << "ptrValue_1 = " << ptrValue_1 << endl;
cout << "global_ptr = " << global_ptr << endl;
return 0;
}
The idea here is to show you that both the address and the pointer’s value will change. The output of the above code is as follows.
Value_1 = 10
Value_2 = 20
ptrValue_1 = 0x4b8000
global_ptr = 0x4b8004
Value_1 = 40
Value_2 = 50
ptrValue_1 = 0x4b8004
global_ptr = 0x4b8004
Handle Values by Pointer to Pointer Reference (Double Pointer) in C++
It will work the same as the single pointer reference. The only difference is that it is a double-pointer.
For instance, if you pack a gift inside one box and pack the same gift inside the box in a box, eventually, the other person will have the gift. It does not make a difference.
The only difference will be in the syntax. The double-pointer reference goes as follows.
int **ptrToptr = &ptrValue_1;
Let’s take a look at the function/method.
void bypointerPointerReference(int**& ptr) {
**ptr = 40;
*ptr = global_ptr;
**ptr = 50;
}
There’s no difference except for the syntax. The main function will be as follows.
int main() {
// a double pointer to value 1
int *ptrValue_1 = &value_1;
int **ptrToptr = &ptrValue_1;
// a global pointer having an address of value 2
global_ptr = &value_2;
// values before calling function
cout << "Value_1 = " << value_1 << endl;
cout << "Value_2 = " << value_2 << endl;
cout << "ptrValue_1 = " << ptrValue_1 << endl;
cout << "global_ptr = " << global_ptr << endl;
bypointerPointerReference(ptrToptr);
// values after calling function
cout << "Value_1 = " << value_1 << endl;
cout << "Value_2 = " << value_2 << endl;
cout << "ptrValue_1 = " << ptrValue_1 << endl;
cout << "global_ptr = " << global_ptr << endl;
return 0;
}
The output will be the same as the one above.
Value_1 = 10
Value_2 = 20
ptrValue_1 = 0x4b8000
global_ptr = 0x4b8004
Value_1 = 40
Value_2 = 50
ptrValue_1 = 0x4b8004
global_ptr = 0x4b8004
Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.
LinkedIn