I just studying pointers in C and I wondered what is the address of a pointer and after I searched on the Internet I wrote this piece of code.
#include <stdio.h>
int main() {
int n = 60;
int *p = &n;
int **p_p = &p;
printf("%p\n", *p_p);
printf("%p", &p);
return 0;
}
and I expected two outputs which are the same. Could anyone explain why outputs differ?
A pointer is a variable like any other, as such it needs to be stored, the address of a pointer is the address of the memory location where the pointer is stored.
The value of the pointer is the address of the variable it points to so the value of
pis the address ofnand the value ofp_pis the address ofp.When you dereference
p_pyou get the value ofpnot the address ofp, and as the value ofpis the address ofnthe value of*p_pis the address ofn.So
will print the exact same value, the address of
n.Printing
&pwill print the address ofp.