Why can't you add pointers in c?

  • Thread starter grocery_stocker
  • Start date
R

Rouben Rostamian

Why is it that you can subtract pointers, but you can't add them?

If you and I live on the same street, the difference of our
house numbers is some sort of indication of the distance
between our houses.

Now, you tell me, what meaning is there in the sum of our
house numbers?
 
W

Walter Roberson

If you and I live on the same street, the difference of our
house numbers is some sort of indication of the distance
between our houses.

Only in places that the house numbers have been rationalized
at some point. Which is not the case in important parts of China
I understand -- there a house number might be an indication
of the relative order they were built... or a house number
might have been bought from it's previous owner (recall
that one of the digits there is similar to the word for 'luck'
and one is similar to the word for 'death', so there is an
active market in lucky addresses and phone numbers...)

Then there's the fact that in some places, they don't -have-
house numbers: addresses are descriptive, such as "110 metres north
of the tall tree east of the train station".

Now, you tell me, what meaning is there in the sum of our
house numbers?

If we narrow our focus to places where house numbers exist and
have -some- correlation to the distance between the houses
(watch out for locations that use cross-street relatie addresses) --
then we are more likely than not in a location where the
sum of the house numbers gives us information about whether
the two houses are on the same side of the road -- even sum
for same side.

But I'm having a hard time coming up with anything else the
sum might be useful for...
 
P

pete

Then there's the fact that in some places, they don't -have-
house numbers: addresses are descriptive, such as "110 metres north
of the tall tree east of the train station".

If we subtract the address of the place that is 110 metres north
of the tall tree east of the train station from the address
of the place that is 210 metres north
of the tall tree east of the train station,
we find that they are 100 meters apart.

If we add 210 meters north of the tall tree east
of the train stationto 110 meters north
of the tall tree east of the train station,
we get 320 meters north of the tall tree east of the train station,
which is meaningless.
 
R

Rob Morris

Rouben said:
If you and I live on the same street, the difference of our
house numbers is some sort of indication of the distance
between our houses.

Now, you tell me, what meaning is there in the sum of our
house numbers?
Hmm, you're at risk of starting a debate on how houses are numbered.

Here is a theory, though I'm open to being pulled up by the regulars if
it's silly: surely the use we have for adding and subtracting from
pointers into an object is the relation:

pointer_1 + offset = pointer_2
(address of an element plus some integer distance gives address of some
other element. Obvously the above is an equation, not a C statement).

Using our algebra skills, we can arrive at:
pointer_2 - pointer_1 = offset

But that's about all, isn't it? Adding one pointer to another just
gives us a number way out somewhere in address space. If we want to do
something with a sum of pointers (subtract a pointer or divide) to get
back to something useable, then we always end up with an expression
equivalent to calculating an offset and evaluating pointer_2 = pointer_1
+ offset, don't we?
 
W

Walter Roberson

Walter Roberson wrote:
If we subtract the address of the place that is 110 metres north
of the tall tree east of the train station from the address
of the place that is 210 metres north
of the tall tree east of the train station,
we find that they are 100 meters apart.

But "210 metres north of the tall tree east of the train station"
doesn't exist as an address: that's "The apartment with the red door
on the road east of the train station". And the place next to that,
part of the same apartment block (i.e., the same "object") is
"The apartment with the white fence [etc]". But the place next
to that might be back to so-many metres north, because it was
built before the apartment block went in...
 
G

Gordon Burditt

Why is it that you can subtract pointers, but you can't add them?

If you add two pointers, WHAT DOES THE RESULT POINT AT?

If the result is always NULL, why bother? If the result is always
undefined or performing the operation invokes undefined behavior,
why bother? If the result is not always null and is well-defined,
provide a non-machine-dependent explanation of what the result
points at.

Gordon L. Burditt
 
K

Keith Thompson

grocery_stocker said:
Why is it that you can subtract pointers, but you can't add them?

The real question should be, why can you subtract them?

Pointers (or addresses) are not numbers. (On many systems, they may
be represented as integers that are an index into the system's address
space, but that's an irrelevant implementation detail.) A pointer
value designates some object (or, if it's a null pointer, designates
no object). You can obtain a pointer value by taking the address of
an object or by calling an allocation routine, you can dereference a
pointer value to obtain the value of the object it points to, you can
assign a pointer value to a pointer variable, and you can compare two
pointer values for equality.

In some languages, that's *all* you can do with pointers.

C also allows you to add a pointer value and an integer to obtain a
new pointer value, with the integer specifying the offset from the
original pointer. It also allows the reverse operation, subtracting
one pointer value from another to obtain the difference between them.
These operations are allowed because they're meaningful and useful.

The fact that these arithmetic operations exist does not imply that
pointers are numbers; rather, they're special-case asymmetric
operations that operate on a numeric value and a non-numeric value.
It might even have made sense to provide these operations but use
something other than the "+" and "-" operator symbols for them.

Adding two pointer values is neither meaningful nor useful. If you
think it is, try to construct a case where you could actually use the
result of such an operation.
 
M

Mark McIntyre

On 3 Jun 2005 03:51:29 GMT, in comp.lang.c ,
Only in places that the house numbers have been rationalized
at some point. Which is not the case in important parts of China

Even there, the difference between your house numbers tells you
something about their relative location, provided you know something
about the local numbering scheme.

On the other hand, the sum of two house numbers tells you zero about
anything, in any part of the world (your example notwithstanding,
since it fails in circular streets...)
 
C

CBFalconer

grocery_stocker said:
Why is it that you can subtract pointers, but you can't add them?

Why are you reposting this, in a new thread, about 20 minutes after
your original post? Are you trying to annoy?
 
D

Dik T. Winter

> On 3 Jun 2005 03:51:29 GMT, in comp.lang.c ,
> (e-mail address removed)-cnrc.gc.ca (Walter Roberson) wrote: ....
>
> Even there, the difference between your house numbers tells you
> something about their relative location, provided you know something
> about the local numbering scheme.

Not necessarily. In Japan houses in a district are numbered by age.
So 1 is the oldest building. (Yes, it appears to be very difficult to
find an address when you do not know the district. You better ask
the postman, he probably knows.)
 
K

Kenny McCormack

Keith Thompson said:
Adding two pointer values is neither meaningful nor useful. If you
think it is, try to construct a case where you could actually use the
result of such an operation.

Or, you could look up the definition of "troll" (both as a noun and as
a verb).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top