473,320 Members | 2,158 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,320 software developers and data experts.

What can't you add pointers in c?

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

Nov 14 '05 #1
11 1834
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
I have these errors with code following: --------------------------------------------------------------- 'SharedObject' : use of class template requires template argument list...
3
by: FroxX | last post by:
Hi all, i have a question. What does those ** mean??? For example: void function(Something **pSomething); I hope someone can help me about it. Thanks,
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
140
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
6
by: jason | last post by:
Hello, I have a question about what kind of datastructure to use. I'm reading collumn based data in the form of: 10\t12\t9\t11\n 24\t11\t4\t10\n ..... I now have a structure which allows me...
69
by: Yee.Chuang | last post by:
When I began to learn C, My teacher told me that pointer is the most difficult part of C, it makes me afraid of it. After finishing C program class, I found that all the code I wrote in C contains...
16
by: John Doe | last post by:
Hi, I wrote a small class to enumerate available networks on a smartphone : class CNetwork { public: CNetwork() {}; CNetwork(CString& netName, GUID netguid): _netname(netName),...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.