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

Valid operations on pointers in C

P: n/a
Why division/mulitiplication/addition of pointers are not
allowed in C?
Nov 14 '05 #1
Share this Question
Share on Google+
32 Replies


P: n/a
aruna <ar********@yahoo.co.in> scribbled the following:
Why division/mulitiplication/addition of pointers are not
allowed in C?


First define how it would work, and then we'll see why it should or
should not be allowed.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
Nov 14 '05 #2

P: n/a
"aruna" <ar********@yahoo.co.in> wrote in message
news:a2**************************@posting.google.c om...
Why division/mulitiplication/addition of pointers are not
allowed in C?


Those operations have no obvious meaning, so there is no need.
Nov 14 '05 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

aruna wrote:

| Why division/mulitiplication/addition of pointers are not
| allowed in C?

A pointer can be considered to be the 'location' of a data item.

Subtraction of one pointer from another gives you the 'distance' between
two data items.

What would addition of two pointers give you? (Hint: What does the
addition of '12 Oak Lane' to '757 Main Street West' give you?)

What would multiplication of two pointers give you? (Hint: What does the
multiplication of '12 Oak Lane' with '757 Main Street West' give you?)

What would division of two pointers give you? (Hint: What does the
division of '12 Oak Lane' by '757 Main Street West' give you?)

While pointer subtraction is meaningful, the other operations are not.

- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAiSyFagVFX4UWr64RAo1XAKC8YTdVBF0bWGf93dNiVV zWrhMhsQCeOszA
RKCqbpu4N77+X+8EE5VcwEk=
=+jwy
-----END PGP SIGNATURE-----
Nov 14 '05 #4

P: n/a
Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

aruna wrote:

| Why division/mulitiplication/addition of pointers are not
| allowed in C?

A pointer can be considered to be the 'location' of a data item.

Subtraction of one pointer from another gives you the 'distance' between
two data items.

What would addition of two pointers give you? (Hint: What does the
addition of '12 Oak Lane' to '757 Main Street West' give you?)

What would multiplication of two pointers give you? (Hint: What does the
multiplication of '12 Oak Lane' with '757 Main Street West' give you?)

What would division of two pointers give you? (Hint: What does the
division of '12 Oak Lane' by '757 Main Street West' give you?)

While pointer subtraction is meaningful, the other operations are not.

- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group


One can add a scalar quantity to a pointer to a new location.
For example, "Move 3 houses north of 12 Oak Lane", makes sense.

If two pointers can be subracted for a distance then a distance
should be added from one pointer to generate a new pointer value.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #5

P: n/a
aruna wrote:

Why division/mulitiplication/addition of pointers are not
allowed in C?


Using right forefinger, point to nose. Using left forefinger,
point to right ear. Now define the product, sum, ratios of those
pointers.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6

P: n/a
Thomas Matthews <Th****************************@sbcglobal.net> scribbled the following:
Lew Pitcher wrote:
aruna wrote:
| Why division/mulitiplication/addition of pointers are not
| allowed in C?

A pointer can be considered to be the 'location' of a data item.

Subtraction of one pointer from another gives you the 'distance' between
two data items.

What would addition of two pointers give you? (Hint: What does the
addition of '12 Oak Lane' to '757 Main Street West' give you?)

What would multiplication of two pointers give you? (Hint: What does the
multiplication of '12 Oak Lane' with '757 Main Street West' give you?)

What would division of two pointers give you? (Hint: What does the
division of '12 Oak Lane' by '757 Main Street West' give you?)

While pointer subtraction is meaningful, the other operations are not.
One can add a scalar quantity to a pointer to a new location.
For example, "Move 3 houses north of 12 Oak Lane", makes sense. If two pointers can be subracted for a distance then a distance
should be added from one pointer to generate a new pointer value.


Where did anyone mention a scalar quantity?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You could take his life and..."
- Mirja Tolsa
Nov 14 '05 #7

P: n/a
In <a2**************************@posting.google.com > ar********@yahoo.co.in (aruna) writes:
Why division/mulitiplication/addition of pointers are not
allowed in C?


Try figuring out some *useful* semantics for these operations and you may
understand why.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8

P: n/a
"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
aruna wrote:

Why division/mulitiplication/addition of pointers are not
allowed in C?


Using right forefinger, point to nose. Using left forefinger,
point to right ear. Now define the product, sum, ratios of those
pointers.


This analogy is lost on me. If I subtract one of those pointers from the
other, then what body part would I be pointing to now?

--
Tim Hagan
Nov 14 '05 #9

P: n/a
Tim Hagan <no*****@boeing.com.invalid> scribbled the following:
"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
aruna wrote:
> Why division/mulitiplication/addition of pointers are not
> allowed in C?
Using right forefinger, point to nose. Using left forefinger,
point to right ear. Now define the product, sum, ratios of those
pointers.

This analogy is lost on me. If I subtract one of those pointers from the
other, then what body part would I be pointing to now?


None. But you have the distance between your nose and your right ear. C
works like that too. Now define the product, sum and ratio of those
pointers. Either in real-world or C terms.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
Nov 14 '05 #10

P: n/a
Joona I Palaste wrote:
[...]
This analogy is lost on me. If I subtract one of those pointers from the
other, then what body part would I be pointing to now?


None. But you have the distance between your nose and your right ear. C
works like that too. Now define the product, sum and ratio of those
pointers. Either in real-world or C terms.


The closest thing I can come up with (in real-world, but not C) is
(ptr1+ptr2)/2 for the average. (ie: what is midway between your nose
and right ear.) Of course, this is really the average of two _points_
and not two _pointers_.

And, in C, this can be done (assuming that ptr1 and ptr2 are such that
their difference is defined) with "ptr1 + ( (ptr2-ptr1) / 2 )", which is
more meaningful in a sense. (ie: start here and go halfway to there.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+

Nov 14 '05 #11

P: n/a
Addition / subtraction is definitely allowed, the reason being pointers
refer to address and you can back and forth in the memory space. (Say,
as in an array as the memory is allocated continuously ).

Multiplication / Division just does not make sense, as there is no
necessity for them logically in a given program.

HTH

aruna wrote:
Why division/mulitiplication/addition of pointers are not
allowed in C?

Nov 14 '05 #12

P: n/a
In article <a2**************************@posting.google.com >,
ar********@yahoo.co.in (aruna) wrote:
Why division/mulitiplication/addition of pointers are not
allowed in C?


Tell us what the result would be.
Nov 14 '05 #13

P: n/a
aruna wrote:
Why division/mulitiplication/addition of pointers are not
allowed in C?


Pointer values are addresses. As such they are similar to postal zip
codes or telephone numbers. Of what possible value would be the
division, multiplication or addition of two or more zip codes?

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #14

P: n/a
Rakesh Kumar <dr******************@yahoo.com> scribbled the following:
Addition / subtraction is definitely allowed, the reason being pointers
refer to address and you can back and forth in the memory space. (Say,
as in an array as the memory is allocated continuously ).


Addition of two pointers is not allowed, addition of a pointer and a
scalar is.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
Nov 14 '05 #15

P: n/a
On Fri, 23 Apr 2004 07:28:43 -0700, aruna wrote:
Why division/mulitiplication/addition of pointers are not
allowed in C?


This was covered back in January. See:

<http://groups.google.com/groups?threadm=iyuKb.22609%24lo3.11294%40newsread2 .news.pas.earthlink.net>

- Rob

--
(to email me, remove "warez.")

Nov 14 '05 #16

P: n/a
[top-post rearranged]
aruna wrote:
Why division/mulitiplication/addition of pointers are not
allowed in C?

Rakesh Kumar <dr******************@yahoo.com> wrote:Addition / subtraction is definitely allowed, the reason being pointers
refer to address and you can back and forth in the memory space. (Say,
as in an array as the memory is allocated continuously ).


Addition of pointers is *not* allowed. What should it yield, anyway?

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #17

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Thomas Matthews <Th****************************@sbcglobal.net> scribbled the following:
Lew Pitcher wrote:
aruna wrote:
| Why division/mulitiplication/addition of pointers are not
| allowed in C? <snip> While pointer subtraction is meaningful, the other operations are not.
One can add a scalar quantity to a pointer to a new location.

<snip>Where did anyone mention a scalar quantity?


Pointers *are* scalars. Thomas surely meant to talk about
*arithmetic* (more precisely: integer) values added to or
subtracted from pointer values.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #18

P: n/a
On 24 Apr 2004 05:42:41 GMT, Joona I Palaste <pa*****@cc.helsinki.fi>
wrote:
Addition of two pointers is not allowed, addition of a pointer and a
scalar is.


The last clause contradicts the first. Arithmetic types and pointer
types are collectively called scalar types. If one operand is of
pointer to object type, the other operand must be of integer type.

--

Best wishes,

Bob
Nov 14 '05 #19

P: n/a
О Joona! Откуда ты взялся?

23 апр 04 18:59, you wrote to All:
This analogy is lost on me. If I subtract one of those pointers from
the other, then what body part would I be pointing to now?

JIP> None. But you have the distance between your nose and your right ear.
You can calculate size of an array by calculating pEnd-pFirst.

PS. Sorry for my horrible English =)

Joona? Куда он делся...
Nov 14 '05 #20

P: n/a
On Sun, 25 Apr 2004 01:31:28 +0400, in comp.lang.c ,
To**********@p532.f495.n463.z2.fidonet.org (Tolik Piskov) wrote:
Joona! wrote
This analogy is lost on me. If I subtract one of those pointers from
the other, then what body part would I be pointing to now?
JIP> None. But you have the distance between your nose and your right ear. You can calculate size of an array by calculating pEnd-pFirst.


That was joona's point. Pointer subtraction makes sense, other math
operations don't.

BTW don't use non english character sets on usenet, they don't translate
well.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #21

P: n/a
In article <d0*********************@news20.bellglobal.com>,
Lew Pitcher <Le*********@td.com> wrote:
What would addition of two pointers give you?


Something from which a pointer can be subtracted to get a pointer.

For example:

char a[10], b[10];
char *p = a;
sum_of_two_char_star sum = a + b;

while(1)
{
...;
p = sum - p; /* switch between a and b */
}

-- Richard
Nov 14 '05 #22

P: n/a
In <c6***********@pc-news.cogsci.ed.ac.uk> ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <d0*********************@news20.bellglobal.com>,
Lew Pitcher <Le*********@td.com> wrote:
What would addition of two pointers give you?


Something from which a pointer can be subtracted to get a pointer.

For example:

char a[10], b[10];
char *p = a;
sum_of_two_char_star sum = a + b;

while(1)
{
...;
p = sum - p; /* switch between a and b */
}


When would that be useful? Looks like a solution in search of a problem
to me...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #23

P: n/a
Richard Tobin wrote:
In article <d0*********************@news20.bellglobal.com>,
Lew Pitcher <Le*********@td.com> wrote:

What would addition of two pointers give you?

Something from which a pointer can be subtracted to get a pointer.

For example:

char a[10], b[10];
char *p = a;
sum_of_two_char_star sum = a + b;

while(1)
{
...;
p = sum - p; /* switch between a and b */
}


Well that is interesting. When adding two char pointers to obtain an
integer (I assume that's what you'd get), what must the type and
range of the integer be? I haven't checked the Standard for this
sort of thing. I doubt it is defined. You're teasing, right?

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #24

P: n/a
In article <x9********************@comcast.com>,
Joe Wright <jo********@comcast.net> wrote:
Well that is interesting. When adding two char pointers to obtain an
integer (I assume that's what you'd get)
No no no... you'd get a type quite distinct from pointers and integers.
I haven't checked the Standard for this sort of thing.


Very wise.

-- Richard
Nov 14 '05 #25

P: n/a
In article <c6**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
When would that be useful?
I gave an example: swapping between two pointers. I have several
times used the same technique to swap between two integers. But:
Looks like a solution in search of a problem to me...


The point was not to show that it was necessary, or even a good idea,
but to refute the claim that pointer addition is necessarily nonsensical.

-- Richard
Nov 14 '05 #26

P: n/a
In <c6**********@pc-news.cogsci.ed.ac.uk> ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <c6**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
When would that be useful?


I gave an example: swapping between two pointers. I have several
times used the same technique to swap between two integers. But:


It's not a good idea for swapping between two integers, either: undefined
behaviour if the addition overflows, while the usual method is perfectly
safe. And it works for pointers, too...
Looks like a solution in search of a problem to me...


The point was not to show that it was necessary, or even a good idea,
but to refute the claim that pointer addition is necessarily nonsensical.


If it's neither necessary nor a good idea, you have yet to prove that it
is not nonsensical.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #27

P: n/a
In article <c6**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
It's not a good idea for swapping between two integers, either: undefined
behaviour if the addition overflows,
If the integers are 1 and 2, this rarely happens.
If it's neither necessary nor a good idea, you have yet to prove that it
is not nonsensical.


Since when did something have to be necessary or a good idea in order
to not be nonsensical?

-- Richard
Nov 14 '05 #28

P: n/a
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <c6**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
It's not a good idea for swapping between two integers, either: undefined
behaviour if the addition overflows,


If the integers are 1 and 2, this rarely happens.


If the objects swapped are pointers, you have no idea how "large" they
are, so you cannot be so certain. So much for pointer addition. As for
the same trick with integers, why confuse the optimiser with code which
is hard to read even for humans?

Richard
Nov 14 '05 #29

P: n/a

"Robert Clark" <cl***@warez.exiter.com> wrote in message news:pa****************************@warez.exiter.c om...
On Fri, 23 Apr 2004 07:28:43 -0700, aruna wrote:
Why division/mulitiplication/addition of pointers are not
allowed in C?
This was covered back in January. See:

<http://groups.google.com/groups?threadm=iyuKb.22609%24lo3.11294%40newsread2 .news.pas.earthlink.net>

- Rob


Here is the reproduction:

[BEGIN]

Hi,

Why multiplication of pointers is not allowed?
Till now I only know this, but not the reason why!

PS: As a rule, I searched the FAQ, but could not
find an answer.

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
On Tue, 6 Jan 2004 12:37:08 +0530, "Vijay Kumar R Zanvar"
<vi*****@hotpop.com> wrote:
Hi,

Why multiplication of pointers is not allowed?
Till now I only know this, but not the reason why!

PS: As a rule, I searched the FAQ, but could not
find an answer.
Adding an int to a pointer results in pointing to something a
specified distance further to the "right" in memory.

Subtracting an int from a pointer results in pointing to something a
specified distance further to the "left" in memory.

Subtracting one pointer from another results in how far apart the two
memory locations are.

If your program and data were to be magically relocated as a unit in
memory, each of the above expressions would still produce the same
result.

Until you can define a concept of either adding two pointers or
multiplying two pointers that meets the constraint in the previous
paragraph, the two operations make no sense. (Hint: others have
thought this through and decided such a definition is either not
possible or of no programming value.)
<<Remove the del for email>>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~

Vijay Kumar R Zanvar wrote:
Hi,

Why multiplication of pointers is not allowed?
Till now I only know this, but not the reason why!

PS: As a rule, I searched the FAQ, but could not
find an answer.


The result would be meaningless. An easy (but
informal) way to understand this is to use an analogy:
think of pointer values as street addresses. Then
the following operations make sense:

- Find the house at "123 Main Street."

- To find the neigboring house, compute "123
Main Street plus one." (I'm ignoring such
real-world intrusions as odd-even numbering
schemes, discontinuities between city blocks,
and so on: we're just exploring an imperfect
analogy, after all.)

- To find the distance between two Main Street
addresses, compute "123 Main Street minus 189
Main Street," yielding "minus 66 lots."

However, some other arithmetical operations make
no sense at all:

- Computing "123 Main Street plus 207 Main Street"
produces no useful answer.

- Computing "123 Main Street minus 123 Elm Street"
produces no useful answer.

- Similarly, computing "123 Main Street times
89 El Camino Real" makes no sense. Perhaps the
result might be considered a kind of "area,"
but there seems to be no useful analogous
concept to "area" in the addressing of memory-
resident objects.

- Finally, computing "123 Main Street times three"
might possibly make sense, arriving at "369 Main
Street." But such a definition carries a built-
in assumption that Main Street is zero-based,
and in a linear computer memory no more than one
object can begin at "address zero." If you want
to find the house three times as far from the
start of the street, you really want to compute
"Main Street Origin plus three times (123 Main
Street minus Main Street Origin)."

--
Er*********@sun.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
[END]
Nov 14 '05 #30

P: n/a
In article <c6**********@sunnews.cern.ch> Da*****@cern.ch (Dan Pop) writes:
In <c6**********@pc-news.cogsci.ed.ac.uk> ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:

....
When would that be useful?


I gave an example: swapping between two pointers. I have several
times used the same technique to swap between two integers. But:


It's not a good idea for swapping between two integers, either: undefined
behaviour if the addition overflows, while the usual method is perfectly
safe. And it works for pointers, too...


But that sum idea can be used in linked lists. Just put in the link
field the sum of the back and forward pointer and you can easily walk
both ways (because you know where you came from).
--
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 #31

P: n/a
"Dik T. Winter" <Di********@cwi.nl> wrote:
In article <c6**********@sunnews.cern.ch> Da*****@cern.ch (Dan Pop) writes:
> In <c6**********@pc-news.cogsci.ed.ac.uk> ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:

...
> >>When would that be useful?
> >
> >I gave an example: swapping between two pointers. I have several
> >times used the same technique to swap between two integers. But:

>
> It's not a good idea for swapping between two integers, either: undefined
> behaviour if the addition overflows, while the usual method is perfectly
> safe. And it works for pointers, too...


But that sum idea can be used in linked lists. Just put in the link
field the sum of the back and forward pointer and you can easily walk
both ways (because you know where you came from).


Ew. Vade retro...

Richard
Nov 14 '05 #32

P: n/a
In article <40*****************@news.individual.net>,
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
>It's not a good idea for swapping between two integers, either: undefined
>behaviour if the addition overflows,
If the integers are 1 and 2, this rarely happens.
If the objects swapped are pointers, you have no idea how "large" they
are, so you cannot be so certain.


Obviously the sum-of-two-pointers type would have to be big enough
for the sum of two pointers.

-- Richard
Nov 14 '05 #33

This discussion thread is closed

Replies have been disabled for this discussion.