Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!henry From: henry@utzoo.UUCP (Henry Spencer) Newsgroups: comp.lang.c Subject: Re: incrementing after a cast Message-ID: <7376@utzoo.UUCP> Date: Thu, 4-Dec-86 13:47:03 EST Article-I.D.: utzoo.7376 Posted: Thu Dec 4 13:47:03 1986 Date-Received: Thu, 4-Dec-86 13:47:03 EST References: <349@apple.UUCP> Organization: U of Toronto Zoology Lines: 21 Keywords: cast, increment, postincrement > char *chp; > L = *((sometype *) chp)++; > >The idea of the cast is to force chp to be incremented by the size of >sometype. [But K&R says it's illegal.] It is illegal. There are messier ways of getting around that, but you are making a fundamental mistake regardless. What you are saying is "treat chp as a sometype pointer, and increment it". There is no guarantee that the "increment a sometype pointer" operation bears any relation to the "add n to a char pointer" operation. The two kinds of pointers may not even be the same size! If you wish to do this cleanly, the following is not guaranteed but is more likely to do the right thing on a wide variety of machines: char *chp; L = *(sometype *)chp; chp += sizeof(sometype); -- Henry Spencer @ U of Toronto Zoology {allegra,ihnp4,decvax,pyramid}!utzoo!henry