C++

Pointer to Pointer in C++

This article is about the pointer to pointer concept in C++. The pointer to pointer points or stores the address of another pointer and enables the manipulation of the pointers themselves. Using this concept, we can easily modify a pointer from another location in the memory. Double pointers are beneficial in dynamically allocated memory or multi-dimensional arrays to manipulate the elements of an array. We will discuss this pointer to pointer working and usage in C++ with proper examples.

Scenario 1:  Memory Representation of Pointer to Pointer

In this scenario, declaring the double pointer is similar to the pointer declaration with an additional asterisk (*) before the pointer name. We can easily represent the memory location of a double pointer in C++. The code snippet of the pointer to pointer is given in the following:

#include <iostream>
using namespace std;
int main ()
{
  int digit  = 50;
  int *ptrr;
  ptrr = &digit;
  int **ptrr1;
  ptrr1= &ptrr;
  cout<<"The pointer memory address is: \n";
  cout<<"ptrr (pointer): "<<ptrr<<"\n";
  cout<<"*ptrr1 (double pointer): "<<*ptrr1<<"\n";  
  cout<<" The value stores in pointer is: \n";
  cout<<"*ptrr = "<<*ptrr<<endl;
  cout<<"**ptrr1 (pointer to pointer) = "<<**ptrr1<<endl;
  return 0;
}

 
In the main function, we take a variable whose memory address needs to be stored in a pointer. Now, we initialize the “digit” variable. After that, we declare the “ptrr” pointer that stores the “digit” memory address. Now, we declare the double pointer whose name is “**ptrr1” that stores the address of the “*ptrr” pointer. At the end of the code, we display the memory and value of the pointer and double pointer on the console screen. The output of this code is mentioned in the following:


The memory address of the “ptrr” pointer is “0x6ffe04”, and the “*ptrr1” pointer also stores the memory address of the “ptrr” pointer. The value that is stored inside the pointer is “50”. Basically, the address of the double pointer is always the same as the memory address of the pointer.

Scenario 2:  Pointer to Pointer as Function Parameter

In this scenario, we will learn how to pass the double pointer in any function as a parameter to perform the temporary memory allocation in any variable. The code snippet of the function parameter with double pointer is mentioned in the following:

#include<iostream>
void getMemoryAddress(int **double_ptr) {
    int tempp = 200;
    *double_ptr = &tempp;
}

int main() {
    int *ptr_1;
    int **double_ptr;
    double_ptr = &ptr_1;
    getMemoryAddress(double_ptr);
    std::cout << "Value of **double_ptr is : " << **double_ptr << std::endl;
    return 0;
}

 
Here, we will learn how the pointer to pointer concept works in C++. Remember that one pointer is declared in the program to work with a double pointer. So, we build the “getMemoryAddress” function. We design this function so that when we pass the parameter, it automatically gets the memory address of the double pointer.

In the function, we take the “tempp” variable and the “**double_ptr” double pointer. We pass the address of the specified variable which is “tempp” to the double pointer and the double pointer values as an argument of the function. The program displays the result of the main function code on the console screen, so all the things that are in the main function are executable. We take the “ptr_1” pointer and the double pointer as “double_ptr” in the main function. We pass the address of the pointer to the double pointer.

Now, we pass the double pointer variable in the override function and pass the pointer to the pointer variable in the “cout” output stream statement to show the result of the double pointer.

When the compiler reaches the override function, the compiler checker where this function is defined executes the code inside the function and returns the result to the main function.

The output of this code is attached in the following:


Result: The value of the double pointer is 200.

Scenario 3:  Using the 2D Array with Pointer to Pointer

In this example, we will deal with a 2D array having a double pointer. We take an array and pass the address of an array in the pointer. The complete code of this scenario is provided as follows:

int main() {
    const int rows = 3;
    const int cols = 2;
    int **matrix = new int *[rows];
    for (int i = 0; i < rows; ++i) {
        matrix[i] = new int[cols];
    }
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            matrix[i][j] = i * cols + j;
        }
    }
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            cout << matrix[i][j] << " ";
        }      
        cout << endl;
    }
    for (int i = 0; i < rows; ++i) {
        delete[] matrix[i];
    }
    delete[] matrix;
    return 0;
}

 
As we all know, we have many rows and several columns in a 2D array. In the main function, we initialize the rows and columns that have “const int”. After that, we allocate the memory space for the rows and the memory space for the columns along every row. We pass the number of rows value as a pointer in the matrix double pointer as “**matrix”. In this double pointer, the loop of the number of rows is executed or true. Then, another inner loop is executed until the condition turns false.

After the memory allocation, we assign a value in an array again: an outer loop for the rows and an inner loop for the columns of the 2D array. In the inner loop, the value of rows and columns is assigned to the double pointer and performs a required arithmetic operation. We display the values of a 2D array like the number of rows and columns that are allocated in the memory. The number of rows and columns always points to the double pointer that stores the rows and column values. In the end, we clear up the memory and deallocate this array from the memory in C++.

The output of the 2D array with a double pointer is attached in the following:

Scenario 4:  Swapping the Pointers Using Pointer to Pointer

Here, we will learn how to swap the pointers in C++ by declaring the double pointer. The code snippet of this scenario is attached in the following:

#include <iostream>
void swap(int **ptrr_1, int **ptrr_2) {
    int *temp_var = *ptrr_1;
    *ptrr_1 = *ptrr_2;
    *ptrr_2 = temp_var;
}
int main() {
    int x = 15, y = 25;
    int *ptrrA = &x, *ptrrB = &y;
    std::cout << "Before swap: *ptrrA is = " << *ptrrA << ", *ptrrB is = " << *ptrrB << std::endl;
    swap(&ptrrA, &ptrrB);
    std::cout << "After swap: *ptrrA  is = " << *ptrrA << ", *ptrrB  is= " << *ptrrB << std::endl;
    return 0;
}

 
First, we build the swap function, passing both pointers as the function argument. In the swap function, we take the “temp” pointer and pass the value of “pointer1” in “temp” for some time. Then, we pass the value of “pointer2” to “pointer1”. In the end, we pass the value of the “temp” pointer to the “pointer2”.

In the main function, we need two pointers that we pass or override in the “swap” function. We pass the addresses of variables to the given pointers. Then, the pointer’s value before and after swapping the pointer is displayed.

The output of this code is attached in the following:


As we can see, the pointer’s values are swapped successfully using a double pointer in C++.

Conclusion

We concluded that the pointer to pointer always stores the memory address of any pointer in C++. We can use the double pointer to temporarily use the memory location of any pointer at any moment. This is a very effective way to manipulate the memory address indirectly and approach the data.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content