c programming style - READ THIS
Wayne Throop
throopw at rtp47.UUCP
Sat Jul 20 07:23:46 AEST 1985
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
<the-known-world>!mcnc!rti-sel!rtp47!throopw
More information about the Comp.lang.c
mailing list