Valid operations on pointers in C

J

Joona I Palaste

aruna said:
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 ([email protected]) ------------- 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
 
L

Lew Pitcher

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

iD8DBQFAiSyFagVFX4UWr64RAo1XAKC8YTdVBF0bWGf93dNiVVzWrhMhsQCeOszA
RKCqbpu4N77+X+8EE5VcwEk=
=+jwy
-----END PGP SIGNATURE-----
 
T

Thomas Matthews

Lew said:
-----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
 
C

CBFalconer

aruna said:
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.
 
T

Tim Hagan

CBFalconer said:
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?
 
D

Dan Pop

In said:
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
 
J

Joona I Palaste

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?
 
J

Joona I Palaste

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

Kenneth Brody

Joona I Palaste wrote:
[...]
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.)
 
R

Rakesh Kumar

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
 
J

Joe Wright

aruna said:
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?
 
J

Joona I Palaste

Rakesh Kumar said:
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 ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
 
I

Irrwahn Grausewitz

[top-post rearranged]

Rakesh Kumar said:
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
 
I

Irrwahn Grausewitz

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
 
R

Robert W Hand

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

Tolik Piskov

ï Joona! ïÔËÕÄÁ ÔÙ ×ÚÑÌÓÑ?

23 ÁÐÒ 04 18:59, you wrote to All:
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? ëÕÄÁ ÏÎ ÄÅÌÓÑ...
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top