C address of the variable address

The only way to get the address of an address in C (opposite to double dereferencing is having an intermediate variable?

eg. I have:

int a; int b; int *ptr_a; int *ptr_b; int **ptr_ptr_a; a = 1; ptr_a = &a; ptr_ptr_a = &(&a); <- compiler says no ptr_ptr_a = &&a; <- still compiler says no ptr__ptr_a = &ptr_a; <- ok but needs intermediate variable 

but you can do the opposite, for example.

 b = **ptr_ptr_a; <- no intermediate variable required 

eg. I do not need to do:

 ptr_b = *ptr_ptr_b; b = *ptr_b; 

why are two operators not symmetrical in their functionality?

+6
source share
7 answers

The operator address returns an rvalue, and you cannot take an rvalue address (see here for an explanation of the differences between r and lvalues).

Therefore, you must convert it to an lvalue by storing it in a variable, then you can take the address of that variable.

+11
source

It is probably easier to explain an example memory layout as follows:

 Var Adr value --------------------- a 1000 1 ptr_a 1004 1000 ptr_ptr_a 1008 1004 1008 1004 1008->1004->1000 You can obviously dereference ptr_ptr_a twice *ptr_ptr == ptr_a, **ptr_ptr_a == 1 

But the variable a has an address (1000), what address should the address mean if it is not a pointer address? &a is the terminal station. So &&a makes no sense.

+4
source

When you request an address using '&', you request an address for something stored in memory. Using '&' 2 times means that you want to get the address of an address that does not make sense.

By the way, if you use an intermediate variable, as you do, you will get the address of the intermediate variable. This means that if you use 2 intermediate variable to compare addresses, they will be different. eg:.

 int a = 0; int* p = &a; int* q = &a; // for now &p and &q are different if (&p == &q) printf("Anormal situation"); else print("Ok"); 
+3
source

the address of the address of something is impossible, because the address of the address will be ambiguous

You can get the address of what contains the address of something, and you could have several cases of this (with different values)

+1
source

No addressee address. You have a box with a number on it. This is the number of the field in the memory sequence. There is no place where these numbers are stored. It will also be extremely recursive, as you can see.

+1
source

If you think for a moment, you will notice that in order to get the address of something, it must be remembered.

If you have a variable

 int a 

he has an address. But this address, if in doubt, is nowhere in memory, so it does not need to have an address. IOW, you can get the address of a variable.

In the other direction, everything is simpler. If you have an address for something, you can dereference it. And if this “something” is a pointer, you can dereference it again. Thus, a double indirect reference is made above.

+1
source

Simply put, only those that are physically stored in computer memory can have an address. Just asking for an address does not mean that the address is stored in memory.

When you have an intermediate pointer, you now store it in memory and take the address of where it is stored.

+1
source

Source: https://habr.com/ru/post/912232/


All Articles