Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!lll-crg!nike!cad!ucbvax!decvax!ima!inmet!rgh From: rgh@inmet.UUCP Newsgroups: net.lang.c Subject: Re: Casting a postdecrement operand Message-ID: <5000043@inmet> Date: Sat, 31-May-86 14:55:00 EDT Article-I.D.: inmet.5000043 Posted: Sat May 31 14:55:00 1986 Date-Received: Tue, 3-Jun-86 22:55:52 EDT References: <114@romp.UUCP> Lines: 29 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