-1

As per textbook .

The address-of operator (§ 2.3.2, p. 52) requires an lvalue operand and returns a pointer to its operand as an rvalue.

int a = 6,c=9;
int *x = &a; //Here x is a lvalue.
x = &c;

As per my knowledge if we can assign , it is lvalue .Then why does textbook says address of return rvalue .Can anyone explain me this in layman language

Nikhil Badyal
  • 1,589
  • 1
  • 9
  • 20
  • 2
    It is rvalue as said in your quote. You can not assign to the result of `&` (e.g. `&a = 5;` is an error) – M.M Oct 21 '19 at 03:45
  • It says `&a` is an rvalue. – ph3rin Oct 21 '19 at 03:45
  • @M.M yes ! it was supposed to be a – Nikhil Badyal Oct 21 '19 at 03:45
  • Which part of the shown code you believe assigns something to `&a`, to support your claim that it's an lvalue? The shown code does not assign anything to `&a`, it assigns `&a` to something else. That's a big difference. – Sam Varshavchik Oct 21 '19 at 03:46
  • @problematicDude In `&a`, `&` is the operator and `a` is the operand. Since `a` is an lvalue, the operator `&` can be used and returns an rvalue that is assigned to `x`. Then, variable `x` can be assigned another value (like in `x = &c`) since it is an lvalue. – Gilles-Philippe Paillé Oct 21 '19 at 03:50
  • Also don't be confused by `"returns a pointer to its operand as an rvalue"`. The *address of* operator doesn't return anything, it provides the *address of* an object (i.e. the memory address where the object is stored). You can't change the memory address, thus it is an *rvalue*. You can change what is stored there, but not the address itself. Know also, `"a pointer is simply a normal variable that stores the address for something else as its value"` No magic. – David C. Rankin Oct 21 '19 at 04:13
  • The immutability of the value isn't really the crux of whether it's an rvalue or not – Lightness Races in Orbit Oct 21 '19 at 21:18

2 Answers2

1

Text book says &a is an rvalue. I.e. value cannot be assigned to &a. Compilation error will occur if we try to compile below code.

int a = 6,c=9;
int *x = &a; //Here x is a lvalue.
&a = x; // Compilation error, as '&a' is an rvalue
denz
  • 9
  • 1
  • This is perhaps a little misleading; immutability is not a core property of rvalues (though it is true that rvalues of built-in type have a sort of enforced immutability to help prevent mistakes) – Lightness Races in Orbit Oct 21 '19 at 21:19
1

As per my knowledge if we can assign

This is not necessarily true in case of class types. But it does always hold for pointers, which you use in the example.

Then why does textbook says address of return rvalue

Well, you cannot assign to it:

&a = &c; // ill formed because &a is rvalue
int *x = &a; // OK. Not an assignment
x = &c;  // OK because x is lvalue
eerorika
  • 232,697
  • 12
  • 197
  • 326