The C Standard defines lvalue as:
An lvalue is an expression (with an object type other than void) that potentially designates an object;64) if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const- qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const- qualified type.
I would like to understand exactly which part of this definition prevents "address-of" expressions such as &ch
from being lvalues. When we take the address of an object, doesn't the address "designate" that object (so that if we had char ch
, then &ch = 10
would be equivalent to ch = 10
)? Or it it that &ch
designates a temporary value, not an object? (Are there such things as temporary objects?)
My confusion initially came about while reading Pointers on C, which provides the following explanation:
The precedence table shows that the
&
operator produces an R-value as a result, and they cannot be used as L-values. But why? The answer is easy. When the expression&ch
is evaluated, where is the result held in the computer? It must be somewhere, but you have no way of knowing where. The expression does not identify any specific loation in the machine's memory, and thus is not an lvalue.
I am confused by the part in bold. Doesn't &ch
identify the location of ch
?
&ch=10
, if valid, would set the address ofch
to 10. The address of variables (and other addressable values) is not under user control in C so not an l-value. – Sharasharai*
. – Sharasharai&ch=10
set the address ofch
to 10, if it were valid? (Also, the rvalue in the example was supposed to be achar
, not anint
. I forgot to change it, sorry.) – Accommodation&ch
is the address ofch
and therefore&ch=10
would be an attempt to assign the address itself. – Skiba&ch
identifies the location ofch
. But we are not talking about the location ofch
. We are talking about the location of&ch
. It is the latter that does not exist. – Federalist