[comp.lang.c] Addition of pointers

vlcek@mit-caf.MIT.EDU (Jim Vlcek) (05/07/89)

In article <4093@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes:
>In article <563@lzaz.ATT.COM>, hutch@lzaz.ATT.COM (R.HUTCHISON) writes:
>> 	midpoint_pointer = (start_pointer + end_pointer) / 2;
>
>You're right. It's a valid operation.

In what sense?  It's invalid C, that's for sure.

All you people who want to be able to add pointers, consider this:
What is the significance of the sum of ``the location of Boston'' and
``the location of Detroit?''  Answer: none.

Let's say you denote ``the location of Boston'' by it's longitude and
latitude, and do likewise for Detroit.  I'll denote these by L_B and
L_D.  Then you hack up a construct which purports to find the midpoint
of the path joining Boston and Detroit:

mid_point = (L_B + L_D)/2;

Now, of course, one must define the division as well as the addition
operator for the object ``location.''  So, you add up the longitudes
and latitudes, and divide by two, right?  Wrong, as anyone familiar
with ``great circle'' navigation will recognize.

My point here is that an abstract algebra has been defined for
operations upon the objects which we call pointers, and there is no
reason to assume that this algebra must be point-for-point analogous
to the algebra of real numbers which we call arithmetic.  Certainly
one would not want to define, as part of this algebra, constructs such
as ``location + location'' or ``pointer + pointer'' which in and off
themselves do not have meaning.  An operator which means ``midpoint''
_can_ be defined, of course; to attempt to denote it as (a+b)/2,
however, is misleading in that it implies that the addition and
division operators individually hold meaning.

The concept of object types, and algebras working upon these types,
should not be unfamiliar to software engineers.  Although I am not
myself a software engineer, I would think that one of the first, and
most important, steps in the development of an application would be to
define the objects used in the application and the operators which
are applied to these objects, and then construct the algebra for these
operators.

Jim Vlcek (vlcek@caf.mit.edu  uunet!mit-eddie!mit-caf!vlcek)

roemer@cs.vu.nl (Roemer Lievaart) (05/09/89)

vlcek@mit-caf.MIT.EDU (Jim Vlcek) writes:

>In article <4093@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes:
>>In article <563@lzaz.ATT.COM>, hutch@lzaz.ATT.COM (R.HUTCHISON) writes:
>>> 	midpoint_pointer = (start_pointer + end_pointer) / 2;
>>
>>You're right. It's a valid operation.

It is not. It is a wrong way to write:
	midpoint_pointer = start_pointer + (end_pointer - start_pointer) / 2;

>In what sense?  It's invalid C, that's for sure.

It's invalid anyway: addition of pointers is in general nonsense.
It may make sense to take the average of two pointers, but not by defining
it in ways of two senseless operations (on pointers): addition and division.
____________________________________________________________________________
Roemer B. Lievaart | "Wa:a:na:a: das Soogo! Schnell!" "GRONF!" "Ich krich die
Amsterdam, Holland | Packung nicht auf!" "Gargll" "Da:s woo ho:o:ste Oisnbohn,
roemer@cs.vu.nl    | Das woissu ja wohl!"    -- Bro:sel, "Werner, Normal Ja!"

dg@lakart.UUCP (David Goodenough) (05/09/89)

From article <2299@mit-caf.MIT.EDU>, by vlcek@mit-caf.MIT.EDU (Jim Vlcek):
> In article <4093@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes:
>>In article <563@lzaz.ATT.COM>, hutch@lzaz.ATT.COM (R.HUTCHISON) writes:
>>> 	midpoint_pointer = (start_pointer + end_pointer) / 2;
>>
>>You're right. It's a valid operation.
> 
> In what sense?  It's invalid C, that's for sure.
> 
> All you people who want to be able to add pointers, consider this:
> What is the significance of the sum of ``the location of Boston'' and
> ``the location of Detroit?''  Answer: none.

Taking this to it's (logical??????) conclusion, is not the point half way
between Boston and Detroit (say Elmira in Upstate NY :-) ) best defined
as:

Start at Boston, and go 1/2 way to Detroit, or in pointer arithmetic:

 	midpoint_pointer = start_pointer + (end_pointer - start_pointer) / 2;

Now, in scalar maths this is the same as the formula above, but pointers
are _NOT_ scalars: adjusting the formula again:

	midpoint_pointer = start_pointer / 2 + end_pointer / 2;

Now, just what the hell is half a pointer???????
-- 
	dg@lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp@xait.xerox.com		  	  +---+

johns@calvin.EE.CORNELL.EDU (John Sahr) (05/13/89)

This subject has gotten beaten around a bit lately, and probably doesn't
need any more beating by me.  However,  I was reminded about of the notion
of an "affine space" which is just like a regular vector space except that
for an affin space, there is no fixed origin.

From V. I. Arnold, _Mathematical methods of Classical Mechanics_,
Springer-Verlag,  1978....

{begin quote, page 4}

	     a               a+b
	xx xo     b      xx xo
	xxxxx   ------>  xxxxx
	 xxx              xxx

	Figure 1   Parallel Displacement

	_Affine n-dimensional space A^n_ is distinguished from R^n in that
there is ``no fixed origin.''  The group R^n acts on A^n as _the group of
parallel displacements_ (figure 1):

	a -> a + b,  a elof A^n, b elof R^n, a + b elof A^n

[Thus the sum of two points in A^n is not defined, but their difference is
defined and is a vector in R^n.]

{end quote}

All the "b's" should be bold, and "elof" is "element of" the little "member
of" sign.  "_some text_" indicates italic emphasis.  R^n is R superscripted
by n.

So, this analogy puts pointers as members of a discrete one dimensional
affine space, and offsets as members of the integers ("discrete R^1").
-- 
John Sahr,       Dept. of Electrical Eng., Cornell University, Ithaca, NY 14853
ARPA: johns@calvin.ee.cornell.edu; UUCP: {rochester,cmcl2}!cornell!calvin!johns