Casting a postdecrement operand

rgh at inmet.UUCP rgh at inmet.UUCP
Sun Jun 1 04:55:00 AEST 1986


> My version of pcc on the IBM RT PC allows the following expression:
>
>	struct abc { char d[500]; };
>	struct cba { char e[200]; };
>	struct cba *cbap;
>
>	((struct abc *)cbap)++;
>
> to increment cbap by 500. It appears that the ANSI standard doesn't say
> anything about the legality of this syntax.  

This is covered in the draft Standard:  (1) a cast does not yield an
lvalue;  (2) the operand of post-inc must be an lvalue.  So the
construct is illegal.

Standard-conforming syntax using the same idea is

	#define LCAST(typeP, lvalue)       ( *( (typeP *) &lvalue ) )
	#define INC_BY_SIZEOF(ptr, typeD)  ( LCAST(typeD *, ptr)++ )

	INC_BY_SIZEOF(cbap, struct abc);

Semantically this works only so long as the type that  ptr  points to
has the same alignment requirement as  typeD ,
and (a more subtle point) as long as  typeP
has the same representation as  ptr .

    Randy Hudson  {ihnp4,cca!ima}!inmet!rgh



More information about the Comp.lang.c mailing list