Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Pointer to Pointer

Earlier we have seen that a pointer variable stores the address of another variable.
A pointer variable can also be used to store the address of another pointer.
The pointer variable that stores the address of another pointer is known as pointer to pointer.

Syntax of pointer to pointer

data_type  **pointer_name;

For better understanding let’s take a small example

int i=5;
int *ptr=&i;

Here *ptr1 is a pointer that points to the integer variable i.

C Programming Language Pointer to Integer i

Now we want another pointer that can store the address of pointer ptr1.
For that we should declare pointer to pointer as shown below:

C Programming Language Pointer to Pointer

Retrieval of data

Now let’s retrieve data from variable i, pointer variable ptr1 & pointer to pointer variable ptr2, using a program.

Example

#include <stdio.h>
int main()
{
    int i=5;
    int *ptr1=&i;
    int **ptr2;
    ptr2=&ptr1;
    printf("\n Address of i = %d",&i);
    printf("\n Value of i = %d",i);
    printf("\n");
    printf("\n Address of ptr1 = %d",&ptr1);
    printf("\n Value of ptr1 = %d",ptr1);
    printf("\n Value of i = %d",*ptr1);
    printf("\n");
    printf("\n Address of ptr2 = %d",&ptr2);
    printf("\n Value of ptr2 = %d",ptr2);
    printf("\n Value of ptr1 = %d",*ptr2);
    printf("\n Value of i = %d",**ptr2);
    return 0;
}

Output

Address of i = 2293324
Value of i = 5

Address of ptr1 = 2293312
Value of ptr1 = 2293324
Value of i = 5

Address of ptr2 = 2293304
Value of ptr2 = 2293312
Value of ptr1 = 2293324
Value of i = 5

There could be a pointer to a pointer to a pointer to a pointer & so on.
There is no limit on how far we can create pointer to a pointer.

Example

C Programming Language Pointer to Pointer to Pointer

Leave a reply