Casting a postdecrement operand

rgh%inmet.uucp at BRL.Arpa rgh%inmet.uucp at BRL.Arpa
Wed Jun 11 02:37:22 AEST 1986


Return-Path: <info-c-request at BRL.ARPA>
Redistributed: Xerox-Info-C^.x at XEROX.ARPA
Received: from BRL-AOS.ARPA by Xerox.COM ; 02 JUN 86 16:42:06 PDT
Received: from brl-smoke.arpa by AOS.BRL.ARPA id a015894; 2 Jun 86 17:10 EDT
Received: from USENET by SMOKE.BRL.ARPA id a005080; 2 Jun 86 16:42 EDT
Newsgroups: net.lang.c
Message-ID: <5000043 at inmet>
Nf-ID: #R:romp.UUCP:114:inmet:5000043:000:919
Nf-From: inmet.UUCP!rgh    May 31 14:55:00 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