K
Keith Thompson
SuperKoko said:Since there is a lot of confusion in that discussion, I'll give rulesZach said:Chris said:Sure. An l-value is an expression that may legally appear on the left-
hand side of the assignment operator, =. An r-value is an expression
that may appear on the right-hand side. The set of r-values is a subset
of the the set of l-values. For example, given:
int a;
int b[50];
The following are l-values (and also r-values):
a
b[10]
Whereas the following are ONLY r-values:
17
a + 5
b
Therefore, the following line of code will fail to compile:
a + 5 = 15;
Hope that helps,
Where are the rules of what may go on each side?
(based on C++).
How are rules based on C++ helpful in comp.lang.c?
First, I'll correct statements of Chris Smith:
int a;
int b[50];
a is an lvalue
b[10] is an lvalue
17 is an rvalue
a+5 is an rvalue
b *is an lvalue*
b is the name of an array object. As an expression of array type,
if it's not the operand of a unary "&" or "sizeof" operator, it is
implicitly converted to a pointer. C99 6.3.2.1p3:
Except when it is the operand of the sizeof operator or the unary
& operator, or is a string literal used to initialize an array, an
expression that has type "array of _type_" is converted to an
expression with type "pointer to _type_" that points to the
initial element of the array object and is not an lvalue.
Before this conversion takes place, b is an lvalue; after the
conversion, it isn't. Since there isn't really a "before" and
"after", I suppose you could think of it as a kind of Schroedinger's
lvalue. }
[...]
lvalues are not rvalues and rvalues are not lvalues... They are totally
different concepts.
The C standard doesn't use the term "rvalue" except in a single
footnote, which says:
What is sometimes called "rvalue" is in this International
Standard described as the "value of an expression".
[...]
If an lvalue-expression is found in a context where an rvalue is
expected, an rvalue-to-lvalue conversion occurs (and this conversion is
not conceptual... Physical things occur... And HERE can happen
segmentation faults).
I think you mean an lvalue-to-rvalue conversion. I suppose this
refers to fetching the value of the designated object.
[...]
Comma operator: It doesn't expect any particular type of value (i.e. it
accepts both lvalues and rvalues with different semantics). [...]
In C, AFAIK, the left operand can be either an rvalue or an lvalue and
the value is discarded (as in C++), but the right operand is expected
to be an rvalue, and the expression yields an rvalue.
In C, the operands are expressions. The left operand is evaluated and
its result is discarded. The right operand is then evaluated and its
result becomes the result of the operation. No need to talk about
"rvalues".
[...]
If you want to know all the C++ rules for conditional expressions, look
at:
http://www.open-std.org/jtc1/sc22/wg21/docs/wp/html/nov97-2/expr.html#expr.cond
And if you want to discuss those rules, I recommend comp.lang.c++.