By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,959 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,959 IT Pros & Developers. It's quick & easy.

What can't you add pointers in c?

P: n/a
How come you can subtract pointers, but you cannot add them?

Nov 14 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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.
grocery_stocker wrote:
How come you can subtract pointers, but you cannot add them?


Nov 14 '05 #2

P: n/a
In article <11**********************@o13g2000cwo.googlegroups .com>,
grocery_stocker <cd*****@gmail.com> wrote:
: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...
--
Are we *there* yet??
Nov 14 '05 #3

P: n/a
grocery_stocker <cd*****@gmail.com> wrote:
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.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #4

P: n/a
In article <11**********************@o13g2000cwo.googlegroups .com>,
grocery_stocker <cd*****@gmail.com> wrote:

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


How come your sense of symmetry applies sometimes, but not others?
--
7842++
Nov 14 '05 #5

P: n/a
On 2 Jun 2005 19:41:52 -0700, in comp.lang.c , "grocery_stocker"
<cd*****@gmail.com> wrote:
How come you can subtract pointers, but you cannot add them?


Define a meaning to the sum of two pointers.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #6

P: n/a
In article <11**********************@o13g2000cwo.googlegroups .com>,
"grocery_stocker" <cd*****@gmail.com> wrote:
How come you can subtract pointers, but you cannot add them?


If you could add them, what would the result be?
Nov 14 '05 #7

P: n/a
grocery_stocker wrote:

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:Th*************@gmail.com>
Nov 14 '05 #8

P: n/a
In article <42***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:
grocery_stocker wrote:
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".
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...
--
Any sufficiently old bug becomes a feature.
Nov 14 '05 #9

P: n/a
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.

--
pete
Nov 14 '05 #10

P: n/a
In article <d7**********@canopus.cc.umanitoba.ca> ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <42***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:
grocery_stocker wrote: ...."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.


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.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #11

P: n/a
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <42***************@spamcop.net>,
Kenneth Brody <ke******@spamcop.net> wrote:
grocery_stocker wrote:

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".


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 ;-)


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
Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.