Adding two pointers
R. Vuurboom
roelof at idca.tds.PHILIPS.nl
Tue May 9 00:39:15 AEST 1989
In article <924 at garcon.cso.uiuc.edu> mcdaniel at uicsrd.csrd.uiuc.edu (Tim McDaniel) writes:
>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).
>
Perhaps more of a mathematical nature.
> [Gives argument showing that if addition is added an equation can
result that has different types...]
The properties stated result in fact from some basic properties present
in arithmetic models: fields. (These models model our
arithmetic or arithmetic is based on these models - take your pick).
These properties include (math books everyone!) associativity, closure,
inverseness (having an inverse),distribution and a null element and other
pleasant niceties .
Lets assume pointer addition (new style). There seems to be a consensus that
this should result in a pointer type. In other words:
P + Q = R (where P,Q,R are pointer types)
also
M + n = P ( where M,P are pointer types, n is an integer)
^
Now a little operator overloading is going on here. The first + and the
second + are conceptually different operators. So lets use '+' for the 2nd
operator.
Thus
M '+' n = P
This is something akin to scalar multiplication of vectors.
Now in "standard" arithmetic models the operation '+' would have to
support distribution i.e.
n '+' (P+Q) = (n'+'P) + (n'+'Q)
Just as 3(a+b) = 3a + 3b.
However the semantics we seem to be aiming for is:
n '+' (P+Q) = (n'+'P) + Q = P + (n'+'Q)
Now this is going to result in a "nonstandard" model with any number of
(for us) strange arithmetic properties. (Left as exercise to the reader :-).
Now theres nothing intrinsically wrong with nonstandard arithmetic models.
However the makers of C in their infinite wisdom (naive innocence?) were
probably just trying to model standard arithmetic. Which I think is as
it should be. Introducing pointer addition with the above semantics would
have introduced nonstandard arithmetic into the language.
Disclaimer: Opinions are my own, for my employers you pay extra.
More information about the Comp.lang.c
mailing list