Pointer To Pointer(Double Pointer) With Example

Share Your Love

The term “pointer-to-pointer” or “double pointer” refers to a pointer that holds the address of another pointer. We’ll learn what a double pointer is, how to declare them, and how to use them in C / C++ programming in this guide.

How To Declare Double Pointer In C/C++?

int **q;

Now here **q is a double-pointer.

Let’s understand the concept of double pointers with the help of the below diagram.

double pointer
Double Pointer

As per the diagram, n is a number that holds the data or value 10. Here *p is a single pointer that holds the address of the n variable. Likewise, q is a double pointer that holds the address of pointer variable p.

So n has a value of 10 and addresses 0x23f. The single pointer p has a value of 0x23f and addresses 0x45d. The double q has a value of 0x45d and address 0x78a.

Now to write a program double-pointer in C programming:

#include<stdio.h>  
int main()
{  
	int n=10;      
	int *p;//pointer to int    
	int **q;//pointer to pointer        
	p = &n;//stores the address of number variable      
	q = &p;    
    printf("Value of n variable is %d \n",n); 
	printf("Address of n variable is %x \n",&n);      
	printf("Address of p variable is %x \n",p);      
	printf("Value of *p variable is %d \n",*p);      
	printf("Address of q variable is %x \n",q);      
	printf("Value of **q variable is %d \n",*q);      
	return 0;  
} 

Output:

Value of n variable is 10
Address of n variable is 0x23f
Address of p variable is 0x45d
Value of *p variable is 0x23f
Address of q variable is 0x78d
Value of **q variable is 10

Now, a below double-pointer program in C++:

#include<iostream>  
using namespace std;

int main()
{  
	int n=10;      
	int *p;//pointer to int    
	int **q;//pointer to pointer        
	p = &n;//stores the address of number variable      
	q = &p;    
    cout<<"Value of n variable is "<<n<<" \n"; 
	cout<<"Address of n variable is "<<&n<<" \n";      
	printf("Address of p variable is "<<p<<" \n";      
	printf("Value of *p variable is "<<*p<<" \n";      
	printf("Address of q variable is "<<q<<" \n";      
	printf("Value of **q variable is "<<*q<<" \n";      
	return 0;  
}  

Output:


Value of n variable is 10
Address of n variable is 0x23f
Address of p variable is 0x45d
Value of *p variable is 0x23f
Address of q variable is 0x78d
Value of **q variable is 10

If you want to improve this article, please contact us.

Share Your Love
Default image
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 343

Newsletter Updates

Enter your email address below to subscribe to our newsletter