Pointer to Pointer

Pointers are used to store the address of an other variable of similar type. Supppose if we store the address of a pointer variable to an other pointer variable then its known as pointer to pointer variable.

The pointer to pointer variable is denoted by ** notation.

Syntax

int **ptr;

The ** indicates that it is an pointer to pointer variable.

Example program for pointer to pointer

#include <stdio.h>

int main() {

    int  a = 100;
	//this can store the address of variable
    int  *ptr1;
	// this can store the address of pointer variable only
    int  **ptr2; 

    ptr1 = &a;
    ptr2 = &ptr1;

    printf("Address of a = %u\n", &a);
    printf("Address of ptr1 = %u\n", &ptr1);
    printf("Address of ptr2 = %u\n\n", &ptr2);

    // below print statement will give the address of <span class='highlighter'>a</span>
    printf("Value at the address stored by ptr2 = %u\n", *ptr2);
    
    printf("Value at the address stored by ptr1 = %d\n\n", *ptr1);

    printf("Value of **ptr2 = %d\n", **ptr2); //read this *(*ptr2)
    return 0;
}

Output

Address of a = 3786724
Address of p1 = 3786728
Address of p2 = 3786732

Value at the address stored by p2 = 3786724
Value at the address stored by p1 = 100

Value of **p2 = 100

If we try to assign the address of a normal variable to the pointer to pointer variable, then it will throw compile time error.


Most Read