Pointers to Pointers in C++

Video Tutorial
FREE
Pointer Variables thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

We know that a pointer stores the address of the variable it is pointing to. The pointer's datatype is the same as the variable's data type, so when we declare a pointer to a pointer, the first pointer stores the address of the variable, and the second pointer stores the address of the first pointer. A pointer to a pointer is also called a double pointer, and it stores the address of the other pointer, which in turn stores the address of the variable it is pointing to.

How to Declare a Pointer to Pointer in CPP?

Declaring a Pointer to a Pointer in C++ or double-pointer is very similar to declaring a single pointer; the only difference is that an extra * is added.

For example :

The general syntax is discussed below:

  • Syntax

Here datatype is the datatype of the single pointer, which in turn is the datatype of the variable whose address this single pointer stores. The data type can be int, float, double,, or char.

For example, if you want to store the address of an int variable, then the datatype of the single pointer would be int, and the double-pointer's data type would also be int.

  • Diagram explaining the concept of Double Pointers

concept of Double Pointers

To understand how a double-pointer or pointer to a pointer in c++ works, consider the above diagram:

  1. First, we have an integer variable age, and its value is 21. It is stored at address 1000 (by default, addresses are hexadecimal numbers, but for simplicity, we consider them as integers).
  2. ptr1 is the integer pointer variable that stores the address of the age variable, i.e., 1000, and its address is 2000. It is declared in the following manner :
  1. ptr2 is another integer pointer variable that stores the address of the ptr1 pointer variable, i.e., 2000, and its address is 3000. It is declared in the following manner :

This is how we declare a double-pointer or pointer to pointer in C++ and make it point to another pointer variable. Here we are talking about double pointers only, but this can be extended up to any extent like pointer to pointer to pointer also, etc.

CPP Program to Demonstrate Pointer to Pointer

  • In the below code, we aim to understand the simple working of double pointer. In the code, we have declared an integer variable age which stores the value 21 and a single pointer variable ptr1 which stores the address of the age variable, and a double-pointer variable ptr2 which stores the address of ptr1. At last, we are printing the value of the age variable using the age variable, single pointer variable, and double-pointer variable.

Output:

  • A practical use case of the double-pointer is in the declaration of the double-pointer, below code explains how to declare a 2d array of size nXm.

Conclusion

  • Pointer to a pointer or double pointer stores the address of another pointer variable.
  • Double pointer is declared in the following manner:
  • We can manipulate or access the value of the variable pointed by the single pointer using the double-pointer.
  • One of the practical applications of the double-pointer is in the declaration of 2d arrays.