incrementing after a cast

bds at mtgzz.UUCP bds at mtgzz.UUCP
Sat Dec 6 01:12:23 AEST 1986


In article <349 at apple.UUCP>, kanner at apple.UUCP (Herbert Kanner) writes:
> 	foo()
> 	{
> 		sometype L;
> 		char *chp;
> 
> 		L = *((sometype *) chp)++;
> 	}
> ...
> Our problem arises with the allegation that K&R makes this construct
> illegal.  On page 214 (syntax summary), the only legal context for ++
> is given to be lvalue++, and (type-name) expression is a case of
> expression which, according to that syntax, cannot be an lvalue.

Your confusion comes, I believe, by assuming that precedence and
grouping rules force one parse to the exclusion of all others
(i.e. that the expression is parsed as *(((sometype *) chp)++) which
is illegal). By parenthesising the expression:

	L = (*((sometype *) chp))++

Then using the rules on page 214 the parse is:

	expression : (type-name) expression
	lvalue : * expression
	expression : lvalue++

Note that this is the parse generated even without the parenthesis.
Happily, it is the one you want.



More information about the Comp.lang.c mailing list