Adding two pointers

Tim McDaniel mcdaniel at uicsrd.csrd.uiuc.edu
Sun May 7 14:29:52 AEST 1989


I've thought of one more argument against pointer addition in C.  It
is of a more philosophical nature (and thus I grant that it's not as
strong as my previous ones).

There are a lot of arithmetic identities that are handy to have
around.  Granted that some don't hold, even with the comparatively-
tame integers excluding overflow.  For example, you can't make all of
the div/mod identities true simultaneously for integers:
        a%b + a/b == a
        0 <= a%b and a%b < abs(b)
        (-a)/b == a/(-b) == -(a/b)

However, one identity that holds (excluding overflow for signed
integers) is
        a + b == c    if and only if    a == c - b

Suppose addition of pointers gives a pointer, such that
        a + b == c
However,
        a == c - b
is erroneous from a type point of view -- the right-hand side is an
integer in C, but the left-hand side is a pointer.

Since pointer subtraction yields an integer in C, its inverse
operation is the addition of a pointer to an integer.  (If you want to
design a language in which pointer subtraction somehow yields another
pointer, go right ahead, but I'll never program in it, and it's not
compatable with C.)

To use the "deck of cards" analogy: in C, the difference between the 9
of clubs and the 7 of clubs is 2.  Not "2 of clubs" -- just plain 2.

As a friend just pointed out (thanks, Paul!), adding the 2 of clubs to
the 7 of clubs just gives you a two-card hand.  You *can't* add cards.
:-)

Pointers are not cards.  Pointers do not have units like "feet".  Any
analogies may work, or they may not -- no guarantees.

--

             Tim, the Bizarre and Oddly-Dressed Enchanter

Center for      |||  Internet, BITNET:  mcdaniel at uicsrd.csrd.uiuc.edu
Supercomputing  |||  UUCP:     {uunet,convex,pur-ee}!uiucuxc!uicsrd!mcdaniel
Research and    |||  ARPANET:  mcdaniel%uicsrd at uxc.cso.uiuc.edu
Development,    |||  CSNET:    mcdaniel%uicsrd at uiuc.csnet
U of Illinois   |||  DECnet:   GARCON::"mcdaniel at uicsrd.csrd.uiuc.edu"



More information about the Comp.lang.c mailing list