Investigate this simple demonstrative program.
#include <stdio.h>
int main(void)
{
int x = 10;
int *p = &x;
printf( "The variable x stores the value %d\n", x );
printf( "Its address is %p\n", ( void * )&x );
printf( "\nThe pointer p stores the address of the variable x %p\n",
( void * )p );
printf( "Dereferencing the pointer p we will get x equal to %d\n" , *p );
printf( "The address of the pointer p itself is %p\n", ( void * )&p );
int **pp = &p;
printf( "\nThe pointer pp stores the address of the pointer p %p\n",
( void * )pp );
printf( "Dereferencing the pointer pp we will get the address stored in p %p\n",
( void * )*pp );
printf( "Dereferencing the pointer pp two times "
"we will get x equal to %d\n", **pp );
int y = 20;
printf( "\nBecause dereferencing the pointer pp\n"
"we have a direct access to the value stored in p\n"
"we can change the value of p\n" );
*pp = &y;
printf( "\nNow the pointer p stores the address of the variable y %p\n",
( void * )p );
printf( "Dereferencing the pointer p we will get y equal to %d\n" , *p );
printf( "The address of the pointer p itself was not changed %p\n", ( void * )&p );
return 0;
}
The program output might look like
The variable x stores the value 10
Its address is 0x7fffd5f6de90
The pointer p stores the address of the variable x 0x7fffd5f6de90
Dereferencing the pointer p we will get x equal to 10
The address of the pointer p itself is 0x7fffd5f6de98
The pointer pp stores the address of the pointer p 0x7fffd5f6de98
Dereferencing the pointer pp we will get the address stored in p 0x7fffd5f6de90
Dereferencing the pointer pp two times we will get x equal to 10
Because dereferencing the pointer pp
we have a direct access to the value stored in p
we can change the value of p
Now the pointer p stores the address of the variable y 0x7fffd5f6de94
Dereferencing the pointer p we will get y equal to 20
The address of the pointer p itself was not changed 0x7fffd5f6de98
That is the variables p
and pp
are both pointers. The difference is that the pointer p
points to an object of the type int
(to the variable x
or y
) while the pointer pp
points to the variable p
that has the type int *
. Dereferencing any of these pointers you will get a direct access to the pointed object and can change it (if it is not a constant object).