0
int m=10;
int *ab=&m;
printf("address of *ab:%p\n",&*ab);
printf("adress of ab:%p\n",&ab);

output:
address of *ab:0x7ffd85fddefc
adress of ab:0x7ffd85fddf00

i understand i am intialising a pointer variable ab(not just ab) and when i am printing address of ab instead of *ab why is it not throwing an error as *"ab" does'nt exist ,only "ab" exists.

0
int m=10;

This creates an integer with the value of 10 somewhere in memory.

int *ab=&m;

This creates a pointer to an int, and sets it to the address of the integer m (that is, the value of ab is the address of m).

printf("address of *ab:%p\n",&*ab);

So two things happen here. First, we dereference ab with *; this is, we actually go to the address held in ab which is where the value of m is stored. Then we get the address of this location with &; namely, the address of m. Hence this is ultimately just the address stored in ab to begin with. It's functionally equivalent to:

printf("value of ab:%p\n",ab);

Finally:

printf("address of ab:%p\n",&ab);

This is the address of ab, where the pointer itself is stored in memory. So at this location you find ab, the value of which is the address of m.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .