Pointer To Pointer (Double Pointer) in C
Pointer To Pointer (Double Pointer) in C
Example
For example, the following declaration declares a "pointer to a pointer" of type int −
int **var;
https://www.tutorialspoint.com/cprogramming/c_pointer_to_pointer.htm 1/6
6/16/24, 1:04 PM Pointer to Pointer (Double Pointer) in C
The following example demonstrates the declaration, initialization, and using pointer
to pointer (double pointer) in C:
#include <stdio.h>
int main() {
// An integer variable
int a = 100;
// Pointer to integer
int *ptr = &a;
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_pointer_to_pointer.htm 2/6
6/16/24, 1:04 PM Pointer to Pointer (Double Pointer) in C
Let us declare a pointer to int type and store the address of an int variable in it.
int a = 10;
int *b = &a;
Example
Here is the complete program that shows how a normal pointer works −
#include <stdio.h>
int main(){
int a = 10;
int *b = &a;
printf("a: %d \nPointer to 'a' is 'b': %d \nValue at 'b': %d", a, b, *b);
return 0;
}
Output
It will print the value of int variable, its address, and the value obtained by the
dereference pointer −
a: 10
Pointer to 'a' is 'b': 6422036
Value at 'b': 10
https://www.tutorialspoint.com/cprogramming/c_pointer_to_pointer.htm 3/6
6/16/24, 1:04 PM Pointer to Pointer (Double Pointer) in C
Let's assume that the compiler also allocates it the address 3000.
Hence, "c" is a pointer to a pointer to int, and should be declared as "int **".
You get the value of "b" (which is the address of "a"), the value of "c" (which is the
address of "b:), and the dereferenced value from "c" (which is the address of "a") −
b: 6422036
Pointer to b is c: 6422024
Value at b: 6422036
Here, "c" is a double pointer. The first asterisk in its declaration points to "b" and the
second asterisk in turn points to "a". So, we can use the double reference pointer to
obtain the value of "a" from "c".
Example
Here is the complete program that shows how a double pointer works −
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_pointer_to_pointer.htm 4/6
6/16/24, 1:04 PM Pointer to Pointer (Double Pointer) in C
int a = 10;
int *b = &a;
printf("a: %d \nAddress of 'a': %d \nValue at a: %d\n\n", a, b, *b);
return 0;
}
Output
a: 10
Address of 'a': 1603495332
Value at a: 10
b: 1603495332
Pointer to 'b' is c: 1603495336
Value at b: 1603495332
Value of 'a' from 'c': 10
We can check it by applying the sizeof operator to the pointers "b" and "c" in the
above program −
https://www.tutorialspoint.com/cprogramming/c_pointer_to_pointer.htm 5/6
6/16/24, 1:04 PM Pointer to Pointer (Double Pointer) in C
Note: The size and address of different pointer variables shown in the above
examples may vary, as it depends on factors such as CPU architecture and the
operating system. However, they will show consistent results.
If you do need to have a pointer to "c" (in the above example), it will be a "pointer
to a pointer to a pointer" and may be declared as −
https://www.tutorialspoint.com/cprogramming/c_pointer_to_pointer.htm 6/6