Difference Between *& and **& in C++
The below table illustrates the key differences between *& and **& in C++.
Feature | *& (Pointer to Reference) | **& (Pointer to Pointer Reference) |
---|---|---|
Definition | *& combines a pointer and a reference, allowing us to dereference a pointer and then immediately take a reference to the resulting value. | **& refers to a reference to a pointer to another pointer, allowing deep manipulation of pointer chains. |
Syntax | int* ptr = & value; | int**& ref = ptrPtr; |
Usage | It is used to modify the value at the pointer’s location indirectly through a reference in functions or complex data structures. | It is used to modify the address stored in a pointer or the pointer via itself, commonly used in dynamic and complex data manipulations. |
Memory Access | It allows manipulation of the value pointed by the pointer, but not the address in the pointer. | It enables control over the address in a pointer and the value at that address, providing deeper manipulation capabilities. |
Common Use | It is commonly used in scenarios where the value needs to be changed without modifying the pointer itself, like function calls where values need to be updated. | It is commonly used in scenarios requiring reassignment or reallocation of memory structures, such as modifying linked lists or dynamic arrays. |
Effect on Pointed Data | It allows for direct modification of the data at the memory address pointed to, without affecting the pointer’s address. | It allows modification of the pointer’s address (i.e., where the pointer is pointing) and the data at the new address. |
Difference Between *& and **& in C++
In C++, the *& (pointer to reference) and **&(pointer to pointer reference) symbols are used in the context of pointers and references for manipulating memory addresses and dealing with complex data structures. While they seem similar, they serve different purposes. In this article, we will learn the difference between *& and **& in C++.