Understanding Double Pointers in C: Pointer to Pointer
Learn about double pointers in C programming, which are used to store the address of another pointer. This chapter explains how double pointers create a chain of pointers, enhancing your ability to manage complex data structures like arrays and dynamic memory allocation.
Pointer to Pointer (Double Pointer) in C
What is a Double Pointer in C?
A double pointer, or a pointer to a pointer, is used in C to store the address of another pointer. In C, a variable that holds the address of another variable is known as a pointer. This can include primary data types, arrays, structs, and even other pointers. When a pointer holds the address of another pointer, it is referred to as a "pointer to pointer" or "double pointer."
Essentially, a double pointer creates a chain of pointers. The first pointer points to the second pointer, which in turn points to the actual value.
Declaration of Pointer to a Pointer
The declaration of a double pointer is similar to that of a single pointer, with the only difference being the use of an additional asterisk (*) before the pointer variable name.
For instance, the following declaration creates a double pointer of type int:
int **var;
When accessing a value that is indirectly pointed to by a double pointer, you need to use the asterisk operator twice.
Example of Pointer to Pointer (Double Pointer)
Below is an example demonstrating the declaration, initialization, and usage of a double pointer in C:
Syntax
#include
int main() {
// An integer variable
int a = 200;
// Pointer to integer
int *ptr = &a;
// Pointer to pointer (double pointer)
int **dptr = &ptr;
printf("Value of 'a' is : %d\n", a);
printf("Value of 'a' using pointer (ptr) is : %d\n", *ptr);
printf("Value of 'a' using double pointer (dptr) is : %d\n", **dptr);
return 0;
}
Output
Value of 'a' is : 200
Value of 'a' using pointer (ptr) is : 200
Value of 'a' using double pointer (dptr) is : 200
How Does a Normal Pointer Work in C?
Let's assume an integer variable "a" is stored at an arbitrary address, say 3000. Its pointer variable "b" is allocated an address of 4000. The following illustrates this:
Syntax
#include
int main() {
int a = 20;
int *b = &a; // Pointer to 'a'
printf("a: %d \nPointer to 'a' is 'b': %d \nValue at 'b': %d\n", a, b, *b);
return 0;
}
Output
a: 20
Pointer to 'a' is 'b': 6422036
Value at 'b': 20
How Does a Double Pointer Work?
Now, let's declare a pointer that can store the address of "b", which is itself a pointer to int, written as "int *". For instance, let's assume the address allocated to "b" is 4000.
Syntax
#include
int main() {
int a = 20;
int *b = &a; // Pointer to 'a'
int **c = &b; // Double pointer to 'b'
printf("b: %d \nPointer to 'b' is c: %d \nValue at b: %d\n", b, c, *c);
printf("Value of 'a' from 'c': %d\n", **c);
return 0;
}
Output
b: 6422036
Pointer to 'b' is c: 6422036
Value at b: 6422036
Value of 'a' from 'c': 20
A Double Pointer Behaves Just Like a Normal Pointer
A double pointer behaves similarly to a normal pointer. The size of a double pointer variable is the same as that of a normal pointer. You can confirm this by using the sizeof operator:
Syntax
#include
int main() {
int *b; // Normal pointer
int **c; // Double pointer
printf("Size of b - a normal pointer: %d\n", sizeof(b));
printf("Size of c - a double pointer: %d\n", sizeof(c));
return 0;
}
Output
Size of b - a normal pointer: 8
Size of c - a double pointer: 8
Multilevel Pointers in C (Is a Triple Pointer Possible?)
Theoretically, there is no limit to how many asterisks can be used in a pointer declaration. If you need a pointer to "c" (from the earlier example), it can be declared as:
int ***d = &c;
Double pointers are commonly used to refer to two-dimensional arrays or arrays of strings.