incrementing after a cast

Chris Torek chris at mimsy.UUCP
Sat Dec 6 19:13:52 AEST 1986


>In article <349 at apple.UUCP> kanner at apple.UUCP (Herbert Kanner) writes:
>>		L = *((sometype *) chp)++;
[is illegal---correct]

In article <2319 at mtgzz.UUCP> bds at mtgzz.UUCP writes:
>Your confusion comes, I believe, by assuming that precedence and
>grouping rules force one parse to the exclusion of all others

That is a reasonable assumption, for they do.

>By parenthesising the expression:
>
>	L = (*((sometype *) chp))++

This is indeed legal.  It is also not what was written, and not what
was desired.

>Note that this is the parse generated even without the parenthesis.

No: casts and `++' have higher precedence than `*'.  The construct
is semantically illegal, but the compiler will not alter it because
of this.

Incidentally, with a more concrete example:

	int L;
	char *cp;

	L = (*(int *)cp)++;

treats `cp' as though it were a pointer to an integer, obtains the
integer to which that points, copies that value into L, then increments
the integer to which that points.  The code generated is roughly:

	mov	cp,reg
	mov	*reg,L
	add	#1,*reg
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list