cast changes meaning of auto-increment?

Doug Gwyn gwyn at smoke.ARPA
Tue Sep 13 23:56:28 AEST 1988


In article <901 at mina.liu.se> mikpe at mina.liu.se (Mikael Pettersson) writes:
>	register char *cp, *oldcp;
>	oldcp = cp = (char *)&i;
>	printf("i == %d, ", *(int *)cp++);		/* should print `27' */
>	printf("and cp increased by %d\n", cp-oldcp);	/* 4 or 1 ? */

>My question is: what should be printed by the second printf, 4 or 1?

1, of course.  It's the difference between the old (char *) and an
incremented-by-one (char *).

>Since a cast doesn't produce an lvalue, ...

This has nothing to do with lvalues.  The indirection operator, the
cast operator, and ++ are right-associative at the same level of
precedence in the C grammar.  The ++ operates on cp before the other
operators in this example.  (However, the incremented value is not
stored into cp until after the original value of cp is used, by the
very definition of the post-increment operator.)



More information about the Comp.lang.c mailing list