How to get address of a pointer in c/c++?
Eg: I have below code.
int a =10;
int *p = &a;
So how do I get addre
int a = 10;
To get the address of a, you do: &a
(address of a
) which returns an int*
(pointer to int)
int *p = &a;
Then you store the address of a in p
which is of type int*
.
Finally, if you do &p
you get the address of p
which is of type int**
, i.e. pointer to pointer to int:
int** p_ptr = &p;
just seen your edit:
to print out the pointer's address, you either need to convert it:
printf("address of pointer is: 0x%0X\n", (unsigned)&p);
printf("address of pointer to pointer is: 0x%0X\n", (unsigned)&p_ptr);
or if your printf supports it, use the %p
:
printf("address of pointer is: %p\n", p);
printf("address of pointer to pointer is: %p\n", p_ptr);