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?
Table of Contents
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.
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:
- Memory Layout of C Program
- Types of Preprocessor Directives in C Language
- How Dangling Pointer Affects Programming?
- What is Double Pointer in C?
- What is Pointers in C?
- How String works in C?
- How to implement Sizeof and Strlen in C?
- How to compare two strings without using strcmp?
- What is the use of Operator Precedence in C?
- What is Storage Classes in C Language?
- What is C Tokens?
- What is Operators in C?