adding pointers

Andrew Koenig ark at alice.UUCP
Wed Jun 7 00:35:03 AEST 1989


Adding one pointer to another makes no sense because pointers
represent a location rather than a displacement.

As an analogy, consider a map.  Locations are places, such
as New York, Boston, etc.  A displacement is the distance and
direction one must travel to get from one location to another.

One can express a displacement as the difference between two
locations.  So, for example, (Boston-New York) represents
the distance and direction one must travel to get from
New York to Boston.

It makes sense to add two displacements.  For example,
(New York-Washington) + (Boston-New York) is pretty
clearly equal to (Boston-Washington).  It also makes sense
to add a displacement and a location.  For example,
New York + (Boston-New York) is just Boston, and
Washington + (Boston-New York) is where you'd be if you
left Washington thinking you were in New York and headed
for Boston -- someplace in the Atlantic south of Long Island.
Where?  I don't know, but if you were to add
(New York-Washington) to that place it would put you in
Boston.

On the other hand, it does not make geometric sense to add
two locations.  What, for example, is New York + Boston?
One could concoct a meaning that would be useful in some
cases -- for example, although I don't know what New York+Boston
is, I do know what (New York+Boston)/2 is -- it's the place
half way between New York and Boston, somehwere around New
Haven.  However, I can express (New York+Boston)/2 as
New York+(Boston-New York)/2 and thereby avoid worrying
about what (New York+Boston) might mean.

In fact, I can easily express the mean of any number of locations
L1, L2, ... Ln.  First I choose an arbitrary location.  For
convenience I'll pick L1.  Then I convert each location to a
displacement, so L1, ... Ln become L1-L1, L2-L1, ... Ln-L1.
I average the displacements and then add back L1.

So the average of L1, ... Ln is

	L1 + (L1-L1 + L2-L1 + ... + Ln-L1) / n

The application of all this to C pointers is left as
an exercise for the reader.

-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list