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 ?
divide two pointers, But We can subtract two pointers. Why ?
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 ?
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 ?
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.
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.
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.
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).
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.![]()
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.
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.
Abhishek Jha 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 ?
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.
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).
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.
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.
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.
Do you think you can't do this with C?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.
Eric Sosman said: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.
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)
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.