Casting a postdecrement operand

Jerry Schwarz jss at ulysses.UUCP
Thu May 29 00:50:38 AEST 1986


In article <114 at romp.UUCP> lwh at romp.UUCP (lwh) writes:
>My version of pcc on the IBM RT PC allows the following expression:
>
>		((struct abc *)cbap)++;
>
>
>to increment cbap by 500. It appears that the ANSI standard doesn't say
>anything about the legality of this syntax.  

"++" requires an lvalue. A cast produces a value but not an lvalue,
so this is not legal in ANSI (or in K&R ) C. 

Jerry Schwarz
Bell Labs, MH

Comment:

Many C compilers (especially those derived from pcc) have a tendency
to "extend C by convenience". That is, they have an algorithm that
generates code on correct C programs. They give an error message only
when that algorithmn runs into trouble.  On some illegal programs the
algorithm doesn't run into any problems and so they generate code
anyway.

In this case, I guess that the compiler does not keep track of
whether an expression is an lvalue in the "syntax" rather it detects
it in the "semantics".    In some circumstances it implements a cast
applied to an "identifier"  by changing the data structure to pretend
that the "name" has the modified type.  The combination of these two
results in the behavior you observe.



More information about the Comp.lang.c mailing list