C++ Pointer To Pointer (Double Pointer)
Last Updated :
27 Jan, 2023
In C++ a Pointer is a variable that is used to store the memory address of other variables. It is a variable that points to a data type (like int or string) of the same type and is created with the * operator.
Syntax of a Pointer in C++:
data_type_of_pointer *name_of_variable = & normal_variable;
What is a Pointer to a Pointer or Double Pointer in C++?
Now, we already know that a pointer stores the memory address of other variables. So, when we define a pointer to a pointer, the first pointer is used to store the address of the variables, and the second pointer stores the address of the first pointer. For this very reason, this is known as a Double Pointer or Pointer to Pointer.
The below diagram explains the concept of Double Pointers :
The above diagram shows the memory representation of a Pointer to Pointer or a Double Pointer, we can easily understand that the address of the variable (i.e Address 1) is stored in Pointer 1 and the address of Pointer 1(i.e Address 2) is stored in Pointer 2. This is known as Double Pointers or Pointer to Pointer.
How to Declare a Pointer to a Pointer in C ++?
Declaring a Pointer to Pointer is similar to declaring a pointer in C++. The difference is we have to use an additional * operator before the name of a Pointer in C++.
Syntax of a Pointer to Pointer(Double Pointer) in C++:
data_type_of_pointer **name_of_variable = & normal_pointer_variable;
Example:
int val = 169;
int *ptr = &val; // storing address of val to pointer ptr.
int **double_ptr = &ptr; // pointer to a pointer declared which is pointing to an integer.
The below diagram explains the concept of Double Pointers:
The above diagram shows the memory representation of a pointer to a pointer. The first pointer ptr1 stores the address of the variable and the second pointer ptr2 stores the address of the first pointer.
Below is the C++ Program to implement Pointer to Pointer:
C++
// C++ program to implement
// pointer to pointer
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
int variable = 169;
// Pointer to store the address
// of variable
int* pointer1;
// double pointer to store the
// address of pointer1
int** pointer2;
// Storing address of variable
// in pointer1
pointer1 = &variable;
// Storing address of pointer1
// in pointer2
pointer2 = &pointer1;
// Displaying the value of variable
// with using both single and double
// pointers.
cout << "Value of variable :- " <<
variable << "\n";
cout << "Value of variable using single pointer :- " <<
*pointer1 << "\n";
cout << "Value of variable using double pointer :- " <<
**pointer2 << "\n";
return 0;
}
OutputValue of variable :- 169
Value of variable using single pointer :- 169
Value of variable using double pointer :- 169
What will be the size of a pointer to a pointer in C++?
In the C++ programming language double pointer behave similarly to a normal pointer. So, the size of the variable of the double-pointer and the size of the normal pointer variable is always equal.
Below is a C++ program to check the size of a double pointer:
C++
// C++ program to check the size
// of a pointer to a pointer.
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
int val = 169;
int* ptr = &val;
int** double_ptr = &ptr;
cout << " Size of normal Pointer: " <<
sizeof(ptr) << "\n";
cout << " Size of double Pointer: " <<
sizeof(double_ptr) << "\n";
return 0;
}
Output Size of normal Pointer: 8
Size of double Pointer: 8
Note: The output of the above code also depends on the type of machine which is being used. The size of a pointer is not fixed in the C++ programming language and it totally depends on other factors like CPU architecture and OS used. Usually, for a 64-bit Operating System, a size of 8 bytes memory and for a 32-bit Operating system, a size of 4 bytes memory is assigned.
Time complexity: O(1).
Auxiliary space: O(1).
Similar Reads
Pointer to Pointer in Objective-C
Pointers in Objective-C are a powerful and essential concept for any programmer to master. Pointers allow you to manipulate data stored in memory directly and are used to store the address of a variable. Pointer-to-pointer also known as a double pointer, is a type of pointer that holds the address o
4 min read
What is a Pointer to a Null pointer
NULL pointer in C At the very high level, we can think of NULL as a null pointer which is used in C for various purposes. Some of the most common use cases for NULL are To initialize a pointer variable when that pointer variable isnât assigned any valid memory address yet. C int* pInt = NULL; To che
2 min read
Pointer to an Array | Array Pointer
A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements.Example:C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr;
5 min read
Passing Pointers to Functions In C++
Prerequisites: Pointers in C++Functions in C++ Passing Pointers to functions means declaring the function parameter as a pointer, at the function calling passing the address of the variable and that address will be stored by a parameter that is declared as a pointer. To change the value of any varia
4 min read
C++ - Pointer to Structure
Pointer to structure in C++ can also be referred to as Structure Pointer. A structure Pointer in C++ is defined as the pointer which points to the address of the memory block that stores a structure. Below is an example of the same: Syntax: struct name_of_structure *ptr; // Initialization of structu
2 min read
C++ Pointer Operators
Prerequisite: Pointers in C++ A pointer variable is a variable that stores the address of another variable or in other a pointer variable points to the variable whose address is stored inside it. Syntax: int *pointer_name; There are mainly two types of Pointer operators mainly used: Address of opera
2 min read
Doubly Linked List in C
A doubly linked list is a type of linked list in which each node contains 3 parts, a data part and two addresses, one points to the previous node and one for the next node. It differs from the singly linked list as it has an extra pointer called previous that points to the previous node, allowing th
13 min read
Doubly Linked List in C++
A Doubly Linked List (DLL) is a two-way list in which each node has two pointers, the next and previous that have reference to both the next node and previous node respectively. Unlike a singly linked list where each node only points to the next node, a doubly linked list has an extra previous point
13 min read
Dereference Pointer in C
We know that the pointer variable can store the memory address in C language and in this article, we will learn how to use that pointer to access the data stored in the memory location pointed by the pointer. What is a Pointer? First of all, we revise what is a pointer. A pointer is a variable that
4 min read
void Pointer in C
A void pointer is a pointer that has no associated data type with it. A void pointer can hold an address of any type and can be typecasted to any type.Example of Void Pointer in CC// C Program to demonstrate that a void pointer // can hold the address of any type-castable type #include <stdio.h
3 min read