down

Tim Moore moore%cdr.utah.edu at wasatch.UUCP
Tue Dec 13 16:31:48 AEST 1988


In article <2803 at hound.UUCP> rkl1 at hound.UUCP (K.LAUX) writes:
>In article <45370 at yale-celray.yale.UUCP>, wald-david at CS.YALE.EDU (david wald) writes:

>> You can't assign to a ++ expression.
>	Of course you can!  For example, a simple string copy function:
>copy_string (from, to)
>char *from;
>char *to;
>{
>	while (*to++ = *from++)
>		;
                ^
                |
*to is an lvalue. Therefore you can assign to it. Now, the precedence
of ++ is higher than *, so the pointer contained in (to) is incremented,
but the value of *to++ is still the lvalue that *to represents. That's
why the the idiom above works; you're not really assigning to a ++
expression, but to an lvalue. (Well, OK, you are, but you get the idea.)

The original message contained:
	chcnt++ += (c == '\n');

          ^
          |
        chcnt is an lvalue here, alright, but the result of chcnt++ is
the integer residing in chcnt because "When postfix++ is applied to an
lvalue the result is the value of the object referred to by the
lvalue"(K&R 1st ed., pg 187), which can not be assigned to. This
commandment applies to the *to++ case to(!). to is an lvalue, so to++
evaluates to the pointer that resides in to, which is not an lvalue.
The * operator turns the pointer into an lvalue, so assignment can proceed.


			-Tim Moore
	4560 M.E.B.		   internet:moore at cs.utah.edu
	University of Utah	   ABUSENET:{ut-sally,hplabs}!utah-cs!moore
	Salt Lake City, UT 84112



More information about the Comp.lang.c mailing list