Double Pointer in C (Pointer to Pointer) Explained Simply
A double pointer (int **p) stores the address of another pointer. Understand the two-level dereference with a simple diagram-style example.
What is a double pointer?
int **pp;int x = 10;
int *p = &x; /* p holds address of x */
int **pp = &p; /* pp holds address of p */
Chain picture: pp → p → x. Follow one arrow with one star: *pp gives p, **pp gives x.
The three levels of access
#include <stdio.h>
int main() {
int x = 10;
int *p = &x;
int **pp = &p;
printf("x = %d\n", x); /* direct */
printf("*p = %d\n", *p); /* one jump */
printf("**pp = %d\n", **pp); /* two jumps */
**pp = 50; /* change x through 2 levels */
printf("x = %d\n", x);
return 0;
}
Reading table (memorize this)
| Expression | Gives | Meaning |
|---|---|---|
pp | Address of p | Value stored in pp |
*pp | p (address of x) | One dereference |
**pp | 10 (value of x) | Two dereferences |
Real use 1: Modifying a pointer inside a function
#include <stdio.h>
#include <stdlib.h>
void allocate(int **pp) { /* receives ADDRESS of a pointer */
*pp = (int*)malloc(sizeof(int));
**pp = 99;
}
int main() {
int *p = NULL;
allocate(&p); /* pass address of p */
printf("Allocated value: %d\n", *p);
free(p);
return 0;
}
void insert(struct Node **head, ...).Real use 2: main's argv
int main(int argc, char **argv) /* argv is a double pointer: */
/* pointer to array of char* */
char **argv is an array of string pointers — argv[0] is the program name, argv[1] the first argument. Every C programmer uses a double pointer here, often without realizing it.
Double pointer क्या है?
int **pp;int x = 10;
int *p = &x; /* p में x का address */
int **pp = &p; /* pp में p का address */
Chain picture: pp → p → x. एक star = एक arrow follow करना: *pp देता है p, **pp देता है x.
Access के तीन levels
#include <stdio.h>
int main() {
int x = 10;
int *p = &x;
int **pp = &p;
printf("x = %d\n", x); /* direct */
printf("*p = %d\n", *p); /* एक jump */
printf("**pp = %d\n", **pp); /* दो jumps */
**pp = 50; /* 2 levels से x बदलें */
printf("x = %d\n", x);
return 0;
}
Reading table (याद कर लें)
| Expression | देता है | मतलब |
|---|---|---|
pp | p का address | pp में stored value |
*pp | p (x का address) | एक dereference |
**pp | 10 (x की value) | दो dereferences |
Real use 1: Function के अंदर pointer modify करना
#include <stdio.h>
#include <stdlib.h>
void allocate(int **pp) { /* pointer का ADDRESS receive करता है */
*pp = (int*)malloc(sizeof(int));
**pp = 99;
}
int main() {
int *p = NULL;
allocate(&p); /* p का address pass करें */
printf("Allocated value: %d\n", *p);
free(p);
return 0;
}
void insert(struct Node **head, ...).Real use 2: main का argv
int main(int argc, char **argv) /* argv double pointer है: */
/* char* के array का pointer */
char **argv string pointers का array है — argv[0] program का नाम, argv[1] पहला argument. हर C programmer यहां double pointer use करता है, अक्सर बिना जाने.
Frequently Asked Questions
What is a double pointer in C?
A double pointer (int **pp) is a pointer that stores the address of another pointer, so **pp reaches the final value through two dereferences.
Why do linked list functions take struct Node **head?
Because inserting at the front must change where head points; only receiving the address of head lets the function modify the caller's actual head pointer.
What is char **argv in main?
argv is a pointer to an array of character pointers: each argv[i] is a string, with argv[0] being the program name.