From: Daryle Walker (darylew_at_[hidden])
Date: 2004-10-02 19:29:47
On 9/25/04 6:16 AM, "Victor A. Wagner Jr." <vawjr_at_[hidden]> wrote:
> I don't mean to pick on you in particular. I just got tired of hearing
> people repeat the "mantra" of "you can subtract two pointers, but not add
> them". It's simply NOT true.
Actually, it IS true... From a certain point of view.
That's because pointers are NOT (conceptually) numbers, they're LOCATIONS!
Addition among them makes no sense.
Technically, subtraction between them would be equally senseless. But
memory locations are treated as being linearly arranged in an order.
Therefore we can define subtraction as the OFFSET one location is linearly
So when you take the midpoint of some pointers...
T *a, *b, *c;
..you're taking the average of their offsets (from a defined origin).
T * const base = a;
T * const average = base + ((a - base) + (b - base) + (c - base)) / 3;
> It has also been somewhat of a disappointment that the HLL folks (and C in
> particular (you can program right down to the sand) have managed to ignore
Or maybe they didn't ignore it to the point of an arbitrary mantra, but had
a rationale for not treating pointers as simple numbers.
-- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk