Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!princeton!caip!brl-adm!brl-smoke!rgh%inmet.uucp@BRL.Arpa From: rgh%inmet.uucp@BRL.Arpa Newsgroups: net.lang.c Subject: Re: Casting a postdecrement operand Message-ID: <1240@brl-smoke.ARPA> Date: Tue, 10-Jun-86 12:37:22 EDT Article-I.D.: brl-smok.1240 Posted: Tue Jun 10 12:37:22 1986 Date-Received: Fri, 20-Jun-86 04:22:00 EDT Sender: news@brl-smoke.ARPA Lines: 39 Return-Path: Redistributed: Xerox-Info-C^.x@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@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