When it is amoral... (Re: When is a cast not a cast?)

Guy Harris guy at auspex.auspex.com
Thu May 4 16:55:09 AEST 1989


>It doesn't change the fact that I'd like to be able to add and subtract
>pointers regardless of what trouble I _might_ get into.  Considering the
>general level of danger incurred by programming in something so potentially
>obfuscatory as C, it's a small barrier to remove.

I guess the question is "what do you expect to happen when you add
pointers?"  The definition of "pointer + int" doesn't refer to low-level
bit-banging - i.e., the "multiply by the size of the object and then add
the (binary) values of the pointer and the result of the multiplication"
is perhaps more of a guide to understanding for people unfamiliar with
C-style pointer addition but familiar with bit-banging, and a guide to
the implementer, than a definition.  (Check out the Dec. 7, 1988 draft
- the "multiply by the size of the object..." stuff is relegated to a
footnote.)

Can you come up with a similar definition for the addition of two
pointers? If not, it sounds like it may be implementation-dependent, in
which case C implementations may let you do what you want by casting the
pointers properly (perhaps to some integral type), in which case you
*could* do what you want - you just have to say "pretty please" to the C
compiler.  I have no problem whatsoever with that; I *like* it when the
compiler discovers I've screwed up when it detects a type mismatch - it
saves me from discovering it at run time.  Think of it as a fail-safe.



More information about the Comp.lang.c mailing list