Does a pointer variable also assign a memory address?

In C ++, a simple variable is assigned a memory address on the stack, so we can use a pointer to contain this memory to point to it; then the pointer also assigned a memory address?

If so, can we have a pointer pointer?

+3
source share
3 answers

Yes you are right. We can have pointers to pointers:

int a; int b; int * pa = &a; int ** ppa = &pa; // set a to 10 **ppa = 10; // set pa so it points to b. and then set b to 11. *ppa = &b; **ppa = 11; 

Read it from right to left: ppa is a pointer to a pointer to an int. It is not limited ** . You can have as many levels as you want. int *** will be a pointer to a pointer to a pointer to an int.

I answered a similar question about whether primitive address variables have: Is a memory address a primitive assignment? , the same goes for pointers. In short, if you never take the address of an object, the compiler does not need to assign an address to it: it can store its value in a register.

+15
source

Yes, you can have pointers to pointers (or pointers to pointers to pointers), and yes, if necessary, pointers are also stored at an address in memory.

However, as with all stack variables, the compiler is free to avoid writing data to memory if it can determine that it is not necessary. If you never accept the address of a variable, and it does not need to survive the current region, the compiler can simply store the value in a register.

+12
source

A pointer is just a variable (memory cell) that stores the address of other variables. His own address, of course, could be stored somewhere else.

+2
source

All Articles