Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site rtp47.UUCP Path: utzoo!linus!decvax!mcnc!rti-sel!rtp47!throopw From: throopw@rtp47.UUCP (Wayne Throop) Newsgroups: net.lang.c Subject: Re: c programming style - READ THIS Message-ID: <90@rtp47.UUCP> Date: Fri, 19-Jul-85 17:23:46 EDT Article-I.D.: rtp47.90 Posted: Fri Jul 19 17:23:46 1985 Date-Received: Sun, 21-Jul-85 21:34:43 EDT References: <11570@brl-tgr.ARPA> <935@teddy.UUCP> <1286@uwmacc.UUCP> <2439@sun.uucp> Organization: Data General, RTP, NC Lines: 26 In an otherwise OK article, Guy Harris says: > 1) The only operation in C that adds an integer to the integer > with the same bit pattern as a given pointer and produces a > pointer value with the same bit pattern as the result is > > (pointer_type) ((int)pointer + integer_value) > > Guy Harris This turns out not to be the case. The expression "(int)p" where p is a pointer does not in any way, shape, or form guarantee to yeild an integer with the "same bits" as the pointer p. The "only way" to treat the bits of a pointer as an integer, do arithmetic on this integer, and yeild a pointer with the resulting integer expression's bits is this: union { some_type *p; int i; } u; (u.p=pointer, (u.i += integer_value), u.p); Note that even this code is non-portable, since it assumes the types "int" and "some_type *" are the same size in bits. (There IS no portable way to do it.) -- Wayne Throop at Data General, RTP, NC !mcnc!rti-sel!rtp47!throopw