Learn With Video What is Double Pointer in C ?

Pointers have great significance in programming. Do we already know what pointers are? Let us recall – pointers are variables that are used to store the address of other variables.

Now a pointer to pointer is something where the first pointer is used to store the address of the second pointer. This pointer is known as a Double Pointer in C. Double Pointer is also known as pointer to pointer or pointer-pointer in C programming.

Learn What is Double Pointer in C & How it works with Video?

How to Declare Double Pointer in C?

You can declare double pointers in C just like you would declare a pointer but an extra * is used in this case.

Data_type  **Nameofpointervariable;

For instance,

int **ptr;

How Double Pointers Works in C Programming? 

Suppose we have a variable called x that stores certain values. Now we have a pointer called *ptr1 that stores the address of the variable x.

The Double pointer in C programming,  **ptr2 stores the address of *ptr1. It is like a chain, where **ptr2 points to the memory location containing *ptr1, which in turn points to the memory location named x.

Double pointer in C

Let us take an example for better understanding:

#include <stdio.h>

int main()

{

int x=100; //Initialize Variable ‘x’

int *ptrA; //Declare Single pointer variable ‘ptrA’

int **ptrB; //Declare Double pointer variable ‘ptrB’

ptrA=&x; // pointer ‘ptrA’ holds the address of variable ‘x’

ptrB=&ptrA;  // Double pointer ‘ptrB’ hols the address of ‘ptrA’

printf(“The value of x is: %d\n”, x );

printf(“Value of *ptrA: %d\n”, *ptrA);

printf(“Value of **ptrB: %d\n”, **ptrB);

return 0;

}

Output

The value of x is: 100

Value of *ptrA: 100

Value of **ptrB: 100

Frequently Asked Questions

Q1. What is the need for a double-pointer in C?

Ans: The double-pointer might have a lot of complicated uses in programming. The most popular use is when it is required to change the value that is itself a pointer.

Q2. What is the difference between a double-pointer and a single pointer?

Ans: A single pointer is used to store the address of a variable, but a double pointer is used to store the address of another pointer. A single pointer is declared by a single asterisk * and a double pointer is declared by a double asterisk **.

Also Read:

Hello, My Name is Abhinav. I am an Author in the Education Category of Trickyedu. I have Done My Engineering in Computer Science from DIT University. I have a good command on Science, Programming Language, and microprocessors. So, I choose this platform to share my knowledge and experience.

Leave a Comment