What can't you add pointers in c?

  • Thread starter grocery_stocker
  • Start date
B

baumann@pan

what's meaning the sum of the two pointers?

but subtraction you get the offset of the two pointers. the offset
value may be useful.
 
W

Walter Roberson

:How come you can subtract pointers, but you cannot add them?

You can add pointers and integers (provided that you stay
within the object.)

If you -could- add two pointers, what type would you expect the
result to be? Even presuming we constrain to two pointers
of the same type, and constrain that void* cannot be added,
then if the result was of the same type as the two pointers:

Then a third pointer could be added on, and a fourth pointer,
and unless one placed restrictions on the number of times
the same pointer could be added in, one would then have to
start defining what it meant to multiply pointers... and then
one would have to define what it was to divide pointers;
then you'd want pointer modulo as well. If you are going to
divide pointers, you'd hae to get into rounding and truncation
issues.

And if you've managed to figure out sensible meanings
for all of those, then you've probably also found a sensible
meaning for combining (addtion, multiplication) of pointers
and floating point numbers, and you might as well go ahead
and define pointer logarithms and complex pointer arithmetic
and fourier transforms of pointers...
 
S

S.Tobias

grocery_stocker said:
How come you can subtract pointers, but you cannot add them?

Probably for the same reason your bicycle cannot fly - it wasn't
designed for it, because the makers thought that the majority
would ride it on the ground anyway.

Although in case of pointers the "-" operator is called "subtraction",
what is *really* being subtracted is not the pointers themselves,
but the indices of array elements to which the pointers point to.
 
K

Kenneth Brody

grocery_stocker said:
How come you can subtract pointers, but you cannot add them?

What would such a construct mean?

"127 Main Street" minus "123 Main Street" equals "2 houses".

"127 Main Street" plus "123 Main Street" equals ?????.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
W

Walter Roberson

grocery_stocker wrote:
What would such a construct mean?
"127 Main Street" minus "123 Main Street" equals "2 houses".

Depends. On my street, houses are generally 4 apart numerically.
555 is next to 559.
"127 Main Street" plus "123 Main Street" equals ?????.

When 123 is adjacent to 127, then the result of the addition
would be an apartment block ;-)


One could potentially develop the idea that "adding" pointers
could have the effect of creating a composite object. That is,
current C standards say that any given pointer is allowed to
move through a single object, and the pointer may assume
a value which points immediately after the object (as long
as said pointer is not dereferenced), but setting the pointer
before the object or past 1-after the object is not valid.
In a hypothetical C-like language that allowed "addition" of
pointers, the result might be something that was allowed to
move through the two objects.

[On the flat memory model where pointers are just indices into a block
of virtual memory, this might not make much sense, but since
pointers to different types don't have to be in the same address
space [as long as void* conversions work], a composite-pointer
might start to mean something. Even in the pointer- is-
virtual index case, not all the virtual address space is
necessarily populated, so it might mean something there too.

The only thing I can think of where such a thing might actually
be -useful- has to do with the fact that in C, you are only
allowed to compare pointers if they point to the same object
(or no further than 1 past the object). It is, though, sometimes
useful to be able to compare pointers more generally -- e.g.,
in linked lists, if you want to compare a node pointer to
the list head pointer, then it's certainly not fun to
contemplate that the comparision result is undefined
if the two nodes were allocated in different malloc() calls...
 
P

pete

Walter Roberson wrote:
The only thing I can think of where such a thing might actually
be -useful- has to do with the fact that in C, you are only
allowed to compare pointers if they point to the same object
(or no further than 1 past the object). It is, though, sometimes
useful to be able to compare pointers more generally -- e.g.,
in linked lists, if you want to compare a node pointer to
the list head pointer, then it's certainly not fun to
contemplate that the comparision result is undefined
if the two nodes were allocated in different malloc() calls...

That's not the case.
That restriction is for relational operators.
You can compare the address of any two bytes
from any two objects, for equality.
 
D

Dik T. Winter

>
> Depends. On my street, houses are generally 4 apart numerically.
> 555 is next to 559.

And in the UK there are many streets that start with 1 on one end, number
upwards on the same side, next cross the street where the numbering is
continued. So numbers 1 and 37 could very well be opposite each other,
and 1 next to 2.
 
R

Richard Bos

Depends. On my street, houses are generally 4 apart numerically.
555 is next to 559.


When 123 is adjacent to 127, then the result of the addition
would be an apartment block ;-)

Nope - that's one level of indirection off. *"127 Main" + *"123 Main" is
a block, just as *fp1 + *fp2 == 7.4. "127 Main" + "123 Main" is... well,
perhaps "250 MainMain", or perhaps, on a rectangular grid "250 King", or
goodness and the city planning board know what else.

Richard
 

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