473,385 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why can't you add pointers in c?

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

Nov 14 '05 #1
12 14001
In article <11**********************@f14g2000cwb.googlegroups .com>,
grocery_stocker <cd*****@gmail.com> wrote:
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?

--
Rouben Rostamian

Nov 14 '05 #2
In article <d7**********@pc18.math.umbc.edu>,
Rouben Rostamian <ro****@pc18.math.umbc.edu> wrote:
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...
--
"Mathematics? I speak it like a native." -- Spike Milligan
Nov 14 '05 #3
Walter Roberson wrote:

In article <d7**********@pc18.math.umbc.edu>,
Rouben Rostamian <ro****@pc18.math.umbc.edu> wrote:
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.
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.
Now, you tell me, what meaning is there in the sum of our
house numbers?


--
pete
Nov 14 '05 #4
Rouben Rostamian wrote:
In article <11**********************@f14g2000cwb.googlegroups .com>,
grocery_stocker <cd*****@gmail.com> wrote:
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?

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?

--
Rob Morris: arr emm four four five (at) cam dot ac dot uk
Nov 14 '05 #5
In article <42***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
Walter Roberson wrote:
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.


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...
--
This signature intentionally left... Oh, darn!
Nov 14 '05 #6
>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
Nov 14 '05 #7
"grocery_stocker" <cd*****@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
On 3 Jun 2005 03:51:29 GMT, in comp.lang.c ,
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <d7**********@pc18.math.umbc.edu>,
Rouben Rostamian <ro****@pc18.math.umbc.edu> wrote:
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


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...)
--
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 #9
grocery_stocker wrote:

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?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #10
In article <f1********************************@4ax.com> Mark McIntyre <ma**********@spamcop.net> writes:
On 3 Jun 2005 03:51:29 GMT, in comp.lang.c ,
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:

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


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.)
--
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
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
....
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).

Nov 14 '05 #12
grocery_stocker wrote:
Why is it that you can subtract pointers, but you can't add them?


Can you give an example?

-atl-
--
A multiverse is figments of its own creations
Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
7
by: Sean J. Fraley | last post by:
This code illustrates what I'm confused about: template<typename T> class foo { public: template<typename U> void fooFunction(const foo<U>& x) {
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
42
by: Abhishek Jha | last post by:
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 ?
11
by: grocery_stocker | last post by:
How come you can subtract pointers, but you cannot add them?
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
10
by: danibe | last post by:
I never had any problems storing pointers in STL containers such std::vector and std::map. The benefit of storing pointers instead of the objects themselves is mainly saving memory resources and...
10
by: jklimek | last post by:
I'm currently a Delphi developer (my day job) but at my company we only write custom web application/database stuff, so we never really get into anything advanced. However, I know enough about...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.