12

Is the only way of getting the address of an address in C (opposite to double dereference to have an intermediate variable?

e.g. 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 inverse, e.g.

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

e.g. I don't have to do:

ptr_b = *ptr_ptr_b;
b = *ptr_b;

why are the two operators not symmetrical in their functionality?

bph
  • 10,728
  • 15
  • 60
  • 135
  • 1
    Because pointer must point on some memory. so, `ptr_a = &a` means that ptr_a points on a. but, `&&a` -> there is no 'pointer' on which the pointer can point. ptr_a is some memory on stack, so you can point on it. – nothrow Apr 02 '12 at 12:41
  • There's no reason to complicate things: `int *ptr_ptr_a = &ptr_a;` is fine. – karlphillip Apr 02 '12 at 12:41
  • of course - makes sense after you've pointed that out - many thanks – bph Apr 02 '12 at 12:43

7 Answers7

15

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

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

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
8

It is probably easier to explain with an example memory layout like this:

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_a == ptr_a,  **ptr_ptr_a == 1

But the variable a has an address (1000) what should address of an address mean if it is not the address of a pointer? &ais a final station. Thus &&a wouldn't make any sense.

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
stacker
  • 68,052
  • 28
  • 140
  • 210
4

When you ask for an address using '&', you ask the address for something stored in memory. Using '&' 2 times means you want to get the address of an address which is non-sense.

By the way, if you use intermediate variable as you're doing it, you will get the address of the intermediate variable. Which means 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");
delannoyk
  • 1,216
  • 1
  • 12
  • 22
2

address of address of something is not possible, because an address of address would be ambiguous

You can get the address of something which hold the address of something, and you could have multiple occurances of that (with different values)

stefan bachert
  • 9,413
  • 4
  • 33
  • 40
1

There's no such thing as the address of an address. You have a box, with a number on it. That is the number of the box in the memory sequence. There's no place which stores these numbers. This would also be extremely recursive as you can see.

vascop
  • 4,972
  • 4
  • 37
  • 50
1

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

If you have a variable

int a

it has an address. But this address is, if in doubt, nowhere in memory, so it doesn't need to have an address. IOW, you only can get the address of a variable.

In the other direction, things are easier. If you have the address of something, you can dereference it. And if this "something" is a pointer, you can dereference it again. So the double indirection mentionne above is automatically given.

glglgl
  • 89,107
  • 13
  • 149
  • 217
1

Put simply, only things physically held in the computers memory can have an address. Just asking for the address of something doesn't mean that the address gets stored in memory.

When you have an intermediate pointer, you're now storing it in memory, and taking the address of where it's stored.

Roddy
  • 66,617
  • 42
  • 165
  • 277