[comp.lang.c] lvalues, rvalues

chris@mimsy.UUCP (Chris Torek) (12/14/88)

>In article <45370@yale-celray.yale.UUCP> wald-david@CS.YALE.EDU
>(david wald) writes:
>>You can't assign to a ++ expression.

In article <2803@hound.UUCP> rkl1@hound.UUCP (K.LAUX) writes:
>Of course you can!  For example ...
>	while (*to++ = *from++)

Oh dear, I see it is time for elucidation again.

	*to++ = *from++

does not assign a value to the result of a ++ expression.  It assigns a
value to the result of a unary `*' expression.  The latter is quite
legal.

The names `lvalue' and `rvalue' were invented to describe the
situation.  An `lvalue' is a thing that may appear on the `l'eft side
of an `=' assignment operator---e.g., `*to++'.  An `rvalue' is a thing
that may appear on the `r'ight.  In C, all lvalues are easily and
automatically converted to rvalues as necessary.  The reverse is not
true.

There are only a few things that are lvalues.  For an exhaustive list
(or something exhausting, at any rate) read the draft proposed ANS
standard for C; to remember it yourself, just remember that the obvious
necessities (variables) are lvalues, and that there are only two
operators that can convert something that is not an lvalue into
something that is.  These are unary `*' (indirection) and `->' (pointer
indirection followed by field selection).  All other operators produce
only rvalues, and only L-values may appear on the L-eft of an
assignment operator.  Thus `to++ = *from++' is illegal, but adding
the `*' (which parses as *(to++)---NOT (*to)++) fixes it.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris