C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programming language

Pointer to Pointer (Double Pointer) in C programming language

A pointer variable stores the address of a variable (that must be non-pointer type), but when we need to store the address of any pointer variable, we need a special type of pointer known as "pointer to pointer" or "double pointer".

Thus, double pointer (pointer to pointer) is a variable that can store the address of a pointer variable.

Read: Pointer Rules in C programming language.

Declaration of a pointer to pointer (double pointer) in C

When we declare a pointer variable we need to use dereferencing operator (asterisk character), similarly, to declare pointer to pointer, we need to use two asterisk characters before the identifier (variable name).

Here is an example

int **ptr;

Here, ptr is a pointer to pointer (double pointer); it can store the address of a pointer variable only.

Note: we cannot initialize a double pointer with the address of normal variable; double pointer can be initialized with the address of a pointer variable only.

Initialization of a pointer to pointer (double pointer) in C

We can initialize a double pointer using two ways:

1) Initialization with the declaration

data_type **double_pointer_name= & pointer_name;

2) Initialization after the declaration

data_type **doble_pointer_name;
double_pointer_name = & pointer_name;

Consider the given example

#include <stdio.h>

int main()
{
	int a=10; 	//normal variable
	int *ptr_a;	//pointer variable
	
	//pointer initialization with the 
	//address of a (normal variable)
	ptr_a = &a;	
	
	int **ptr_ptra; //pointer to pointer 
	//pointer to pointer initialization with the 
	//address of ptr_a (pointer variable)
	ptr_ptra = &ptr_a;
	
	printf("value of a: %d\n",a);
	printf("value of a using pointer: %d\n",*ptr_a);
	printf("value of a using pointer to pointer: %d\n",**ptr_ptra);
	
	return 0;
}

Output

value of a: 10
value of a using pointer: 10
value of a using pointer to pointer: 10 








COMMENTS