Pointer to Pointer in C  Programming

Pointer to Pointer (Double Pointer) in C Language:

It is a concept of holding the pointer address into another pointer variable. In C Language, a pointer variable points to a location in memory and is used to store the address of a variable. In C, we can also define a pointer to store the address of another pointer. Such a pointer is known as a double pointer (pointer to pointer). So, when we define a pointer to a pointer, the first pointer is used to store the address of the variable and the second pointer is used to store the address of the first pointer. This is the reason why they are also called double pointers.

Pointer to Pointer in C Language with Examples

In C programming language, the pointer to pointer relations can be applied up to 12 stages but generally, there are no limitations. For a pointer variable, we can apply 12 indirection operators. When we are using the pointer to pointer relations then performance will be decreased.

How to declare a Pointer to Pointer in C?

Declaring Pointer to Pointer or double-pointer is similar to declaring pointer in C. The difference is we have to place an additional * before the name of the pointer.
Syntax: int **ptr;

pointer datatype *ptr;
p2pointer datatype **ptr;
p2p2pointer datatype ***ptr;
p2p2p2pointer datatype ****ptr;

Example to understand Pointer to Pointer in C:
#include<stdio.h>
void main ()
{
    int a = 10;
    int *p;
    int **pp;
    p = &a; // pointer p is pointing to the address of a
    pp = &p; // pointer pp is a double pointer pointing to the address of pointer p
    printf("address of a: %x\n",p); // Address of a will be printed
    printf("address of p: %x\n",pp); // Address of p will be printed
    printf("value stored at p: %d\n",*p); // value stoted at the address contained by p i.e. 10 will be printed
    printf("value stored at pp: %d\n",**pp); // value stored at the address contained by the pointer stoyred at pp
}
Output:
Example to understand Pointer to Pointer in C
Example: Pointer to Pointer in C Language
#include<stdio.h>
int main ()
{
    int i = 10;
    int *ptr;
    int **pptr;
    int ***ppptr;
    ptr = &i;
    pptr = &ptr;
    ppptr = &pptr;
    printf ("\n %d %d %d %d", i, *ptr, **pptr, ***ppptr);
}

Output: 10 10 10 10

For a better understanding of the above program, please have a look at the following diagram.

Pointer-to-Pointer in C Language with Examples
How the logic is evaluated in the above Program:
Pointer-to-Pointer in C
Another Program to Understand Pointer-to-Pointer in C Language:
#include<stdio.h>
void main()
{
    int a1;
    int*ptr;
    int**pptr;
    printf("Enter a value\n");
    scanf("%d", &a1);
    ptr=&a1;
    pptr=&ptr;
    printf("\n%d", *ptr);
    printf("\n%d", **pptr);
}
Output:
Program to Understand Pointer-to-Pointer in C Language
Wild Pointer in C Language:

An uninitialized pointer variable or the pointer which is not initialized with any variable address is called a wild pointer. The wild pointer is also called a bad pointer because without assigning any variable address, it is pointing to a memory location.

Example to Understand Wild Pointer in C Language
void main()
{
    int a;
    int *ptr; //wild or bad pointer
}

When we are working with pointers, always recommended to initialize with any variable address or make it NULL.

Follow Us On