C Double Pointers
In this page:
data_type variable_name = value;
data_type *pointer_name = &variable_name;
data_type **double_pointer = &pointer_name;
**double_pointer; // the original value
What is a Double Pointer?
You can chain this indirection further -- a triple pointer stores the address of a double pointer -- but in practice, C code rarely goes beyond two levels because the added complexity outweighs the benefit.
Example: What is a Double Pointer?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
int *ptr = &x;
int **pptr = &ptr;
// Print formatted text to the screen
printf("%d", **pptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Declaring Double Pointers
int **pptr declares pptr as a pointer to a pointer to an int; the type must match at every level, so you can't assign the address of a plain int pointer to a variable declared with three asterisks.
Example: Declaring Double Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
int *ptr = &x;
int **pptr = &ptr;
// Print formatted text to the screen
printf("%d", **pptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Initializing and Referencing
A typical setup looks like: int x = 5; int *ptr = &x; int **pptr = &ptr -- pptr now holds the address where ptr itself is stored, one level of indirection above x.
Example: Initializing and Referencing
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
int *ptr = &x;
int **pptr = &ptr;
// Print formatted text to the screen
printf("%d", *ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Double Dereferencing ()
Given the setup above, *pptr yields ptr (the address of x), and **pptr yields the value of x itself (5) -- each asterisk peels back exactly one layer of indirection.
Example: Double Dereferencing ()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 5
int x = 5;
int *ptr = &x;
int **pptr = &ptr;
// Print formatted text to the screen
printf("%d", **pptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Practical Uses of Double Pointers
This is essential when a function needs to modify what a pointer points to (not just the value at that address) -- for example, a function that allocates memory and needs the caller's pointer variable itself to be updated to point at the new block.
Example: Practical Uses of Double Pointers
// Include standard input/output (printf, scanf)
#include <stdio.h>
void allocate(int **out) {
static int value = 99;
*out = &value;
}
// Program execution starts in main()
int main() {
int *result;
allocate(&result);
// Print formatted text to the screen
printf("%d", *result);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Dereferencing only once with
*ppand expecting the final value, when**ppis needed to reach the original variable. - Using a double pointer that has not been initialized, so dereferencing it crashes.
- Confusing
*pp(the inner pointer) with**pp(the value it points to).
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: