Home > Back-end >  Grabbing the address of the pointer variable itself, instead of the address that we are pointing to,
Grabbing the address of the pointer variable itself, instead of the address that we are pointing to,

Time:12-28

Let's consider I have the following x pointer variable in my function:

  int* x = new int { 666 };

I know I can print its value by using the * operator:

std::cout << *x << std::endl;

And that I can even print the address of where the 666 is being stored on the heap, like this:

std::cout << (uint64_t)x << std::endl;

But what I'd like to know is on whether it's also possible to grab the address of the x variable itself, that is, the address of the region of memory in the stack containing the pointer to the heap int containing 666?

Thanks

CodePudding user response:

Just use another &

std::cout << (uint64_t)&x << std::endl;

e.g.:

int v = 666; // variable
int * x = &v; // address of variable
int ** p = &x; // address of pointer x

and so on

int *** pp = &p;
int **** ppp = &pp;

and how to access to it:

std::cout << ****ppp << " == " << ***pp << " == " 
    << **p << " == " << *x << " == " << v << std::endl;
  •  Tags:  
  • c
  • Related