Why pointers can only be subtracted ?

A

Abhishek Jha

We cann't add two pointers, We cann't multiply two pointers, We cann't
divide two pointers, But We can subtract two pointers. Why ?
 
J

Jonathan Burd

Abhishek said:
We cann't add two pointers, We cann't multiply two pointers, We cann't
divide two pointers, But We can subtract two pointers. Why ?

Quick! Grab a good book!
Q: What does a pointer variable contain?
A: ________________ (fill in the blanks)

Regards,
Jonathan.
 
I

infobahn

Abhishek said:
We cann't add two pointers, We cann't multiply two pointers, We cann't
divide two pointers, But We can subtract two pointers. Why ?

A pointer gives us a location. Think of it as a street number if
you like (for the purposes, at least, of this explanation).

Consider the addresses 1562 Ram Street and 1838 Ram Street.

If we add these, we get 3400 Ram Street, but what does that mean?
Nothing, really. There may not even be such an address.

If we multiply them, we get 2870956 Ram squared Street squared.
Meaningless.

If we divide one by the other, we get 0. Again, meaningless.

But if we subtract one from the other, we get 276 - the number
of houses you'd have to walk past to get from one to the other
(including one of the end points). This turns out to be a
useful number.

Note that we can usefully do addition of pointers with integers.
For example, 1562 Ram Street + 276 gives us 1838 Ram Street, a
useful address.
 
B

Ben Pfaff

infobahn said:
Consider the addresses 1562 Ram Street and 1838 Ram Street.
[...]

But if we subtract one from the other, we get 276 - the number
of houses you'd have to walk past to get from one to the other
(including one of the end points). This turns out to be a
useful number.

You must live in a place with a different approach to addresses.
Around here, the value you'd get would not be of such
fine-grained meaning. All you could say is that 1838 Ram Street
is probably about 4 blocks from 1562 Ram Street (including the
end blocks).
 
R

Richard Tobin

infobahn said:
Consider the addresses 1562 Ram Street and 1838 Ram Street.

If we add these, we get 3400 Ram Street, but what does that mean?
Nothing, really.

It's easy to assign reasonable meanings to it. For example, it's
something that you can divide by two to find the "average" address
(the midpoint of an array, for example). Or you can alternate between
the two addresses by subtracting the current one from it.

It's not that it's meaningless or useless, it's just not useful enough
to exist in C.

-- Richard
 
I

infobahn

Ben said:
infobahn said:
Consider the addresses 1562 Ram Street and 1838 Ram Street.
[...]

But if we subtract one from the other, we get 276 - the number
of houses you'd have to walk past to get from one to the other
(including one of the end points). This turns out to be a
useful number.

You must live in a place with a different approach to addresses.

Probably, yes.
Around here, the value you'd get would not be of such
fine-grained meaning. All you could say is that 1838 Ram Street
is probably about 4 blocks from 1562 Ram Street (including the
end blocks).

To stretch the analogy still further, we could argue that as far
as comp.lang.c is concerned the subtraction is meaningless unless
the two houses are on the /same/ block. :)
 
R

Richard Bos

infobahn said:
To stretch the analogy still further, we could argue that as far
as comp.lang.c is concerned the subtraction is meaningless unless
the two houses are on the /same/ block. :)

That's not a very far stretch; it is similar to two pointers only being
usefully subtractable if they point to the same object.

Richard
 
I

infobahn

Richard said:
That's not a very far stretch; it is similar to two pointers only being
usefully subtractable if they point to the same object.

Precisely my point.
 
E

Eric Sosman

Richard said:
It's easy to assign reasonable meanings to it. For example, it's
something that you can divide by two to find the "average" address
(the midpoint of an array, for example). Or you can alternate between
the two addresses by subtracting the current one from it.

No, there's a sort of "hidden zero" problem in the
addition. Let's change analogies to escape the peculiarities
of street numbers:

What is the sum of "noon today" and "noon tomorrow?"

What is the sum of "Acme Building, tenth floor" and
"Acme Building, eleventh floor?"

What is the sum of the (nominal) freezing and boiling
points of water? Does your answer depend on the chosen
temperature scale? Why?

What is the sum of "middle C" and "concert A?" Does
it sound good?

Pointers are like numbers that denote positions on a
scale of some kind. It makes sense to subtract two such
positions to find the interval between them, and it makes
sense to add two intervals, or to add a position and an
interval. But it doesn't make sense to add two positions;
one can only do so by agreeing on a zero reference and then
converting one or both positions to intervals.
 
M

Mike Wahler

Abhishek Jha said:
We cann't add two pointers,

Why would you want to? What would their sum signify?
We cann't multiply two pointers,

Why would you want to? What would their product signify?
We cann't
divide two pointers,

Why would you want to? What would the quotient signify?
But We can subtract two pointers. Why ?

The difference between two pointers gives the size
(in bytes) of the portion of memory whose address
begins with the smaller and ends with the larger.

Note that subtraction of pointers is only
valid if both those pointers point within the same
object (or if one or both point one element past the
last element of the same array object).

Subtraction of other pointers gives undefined
behavior.

Which C book(s) are you reading?

-Mike
 
E

Eric Sosman

Mike said:
The difference between two pointers gives the size
(in bytes) of the portion of memory whose address
begins with the smaller and ends with the larger.

The difference is in units of the size of the pointed-
to type, not necessarily in bytes.

AnyType array[2];
assert(&array[1] - &array[0] == 1);

.... even if `sizeof(AnyType)' is 42, or 97, or 4191.
 
C

Chris Croughton

infobahn said:
Consider the addresses 1562 Ram Street and 1838 Ram Street.
[...]

But if we subtract one from the other, we get 276 - the number
of houses you'd have to walk past to get from one to the other
(including one of the end points). This turns out to be a
useful number.

You must live in a place with a different approach to addresses.

By the e-address, infobahn is in Britain, where adresses di often work
like that (although more of them are word addresses, incrementing by two
with odd bytes -- er, houses -- on one side and even ones on the other).
Around here, the value you'd get would not be of such
fine-grained meaning. All you could say is that 1838 Ram Street
is probably about 4 blocks from 1562 Ram Street (including the
end blocks).

Ah, but that's because the pointers refer to different arrays! You have
presumably allocated your arrays in less than 100 elements per array...

(To continue the analogy, 1532 Ram Street minus 2000 may refer to some
house in a totally different street, or possibly a factory or a field,
and will result in undefined (and possibly fatal) behaviour if you try
calling at that address. In the case of your US blocks of houses, the
address 1599 may be between two blocks and you'll get run over by public
transport, which might be referred to as a bus error...)

Chris C
 
R

Richard Tobin

Eric Sosman said:
Pointers are like numbers that denote positions on a
scale of some kind. It makes sense to subtract two such
positions to find the interval between them, and it makes
sense to add two intervals, or to add a position and an
interval. But it doesn't make sense to add two positions;
one can only do so by agreeing on a zero reference and then
converting one or both positions to intervals.

If I take a tape measure and put the end at some arbitrary distance
away, and measure the position of two points (along the line of the
measure) with it, then I can find the average position by adding the
measurements and dividing by two, and I will get the same answer
regardless of where the end of the measure is. Similarly, it works
perfectly well to add two temperatures in Fahrenheit and divide by two
to get the average, and it also works perfectly well in Celsius and
Kelvin, and you get the same answer whichever you use.

If a scale has some kind of zero offset, then of course the sum of two
values will have two of those offsets in it, but provided the meaning
you ascribe to the sum requires you to subtract off a value of the
same kind, or divide by two, before interpreting it on the original
scale, it will all cancel out.

The sum of two pointers doesn't make sense *as a pointer* any more
than the sum of two Fahrenheit temperatures makes sense as a
Fahrenheit temperature. But that doesn't mean it doesn't make sense
at all.

In an object-oriented language you could define a type "sum of two
pointers", whose constructor takes two pointers. Then you could
defined methods on the type such as subtraction of a pointer, which
would return a pointer.

-- Richard
 
P

Peter Nilsson

Richard said:
If I take a tape measure and put the end at some arbitrary distance
away, and measure the position of two points (along the line of the
measure) with it, then I can find the average position by adding the
measurements and dividing by two, and I will get the same answer
regardless of where the end of the measure is. Similarly, it works
perfectly well to add two temperatures in Fahrenheit and divide by two
to get the average, and it also works perfectly well in Celsius and
Kelvin, and you get the same answer whichever you use.

Temperatures are open ended scales. The sum will remain within infinite
range. This may not apply to pointer arithmetic where adding two
pointer values need not produce a value within an address space.

Also, your average can generally be performed with simple pointer
difference...

avg = p + (q - p) / 2;

If it doesn't, then chances are C99's intptr_t might be useful,
if not portable.
If a scale has some kind of zero offset, then of course the sum of two
values will have two of those offsets in it, but provided the meaning
you ascribe to the sum requires you to subtract off a value of the
same kind, or divide by two, before interpreting it on the original
scale, it will all cancel out.

But C pointers (addresses) need not be 'scale' values.
The sum of two pointers doesn't make sense *as a pointer* any more
than the sum of two Fahrenheit temperatures makes sense as a
Fahrenheit temperature. But that doesn't mean it doesn't make sense
at all.

Indeed, there are multiple possible interpretations that pointer
addition _could_ have. But how many of those cannot be implemented
using pointer subtraction (either directly or indirectly)?
In an object-oriented language you could define a type "sum of two
pointers", whose constructor takes two pointers. Then you could
defined methods on the type such as subtraction of a pointer, which
would return a pointer.
Do you think you can't do this with C?
 
M

Mike Wahler

Eric Sosman said:
The difference is in units of the size of the pointed-
to type, not necessarily in bytes.

Are you sure? :) Remember we're talking about the
difference between two pointers, not the result of
subtracting an integer value from a pointer.
AnyType array[2];
assert(&array[1] - &array[0] == 1);

... even if `sizeof(AnyType)' is 42, or 97, or 4191.

Did you test that theory? :)


The difference between &array[1] and &array[0] is
sizeof(AnyType)

-Mike
 
K

Keith Thompson

Mike Wahler said:
Eric Sosman said:
Mike said:
[...]
But We can subtract two pointers. Why ?

The difference between two pointers gives the size
(in bytes) of the portion of memory whose address
begins with the smaller and ends with the larger.

The difference is in units of the size of the pointed-
to type, not necessarily in bytes.

Are you sure? :) Remember we're talking about the
difference between two pointers, not the result of
subtracting an integer value from a pointer.
Yes.

[snip]

The difference between &array[1] and &array[0] is
sizeof(AnyType)

No, it's 1.

C99 6.5.6p9:

When two pointers are subtracted, both shall point to elements of
the same array object, or one past the last element of the array
object; the result is the difference of the subscripts of the two
array elements.

Try this:

#include <stdio.h>

int main(void)
{
typedef struct {
char buf[42];
} AnyType;
AnyType array[2];

printf("&array[1] - &array[0] = %d\n",
(int)(&array[1] - &array[0]));
return 0;
}
 
A

abhishekjha10

Thnx InfoBahn vey much as you have explained using so easy example.
Thanx again.
 

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

Forum statistics

Threads
473,772
Messages
2,569,589
Members
45,100
Latest member
MelodeeFaj
Top