-4
int a, b;
a=10;
&b=a;

This simple code causes the following error when compiled.

error: lvalue required as left operand of assignment

But &b is indeed on the left hand-side. So what does the error message exactly mean?

Resorter
  • 307
  • 3
  • 9

1 Answers1

4

In this context & is the address-of operator. The value of &b is a pointer of type int *. It is a prvalue. This is unrelated to use of the & symbol in a declaration (where it means the declaration of a reference) -- symbols mean different things in declarators than they do in expressions.

You cannot change the address of a variable. Variables have the same address for their whole lifetime.

M.M
  • 138,810
  • 21
  • 208
  • 365