Valid operations on pointers in C

M

Mark McIntyre

On Sun, 25 Apr 2004 01:31:28 +0400, in comp.lang.c ,
Joona! wrote
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.
 
R

Richard Tobin

Lew Pitcher said:
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
 
D

Dan Pop

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

Joe Wright

Richard said:
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?
 
R

Richard Tobin

Joe Wright said:
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
 
R

Richard Tobin

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
 
D

Dan Pop

In said:
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...
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
 
R

Richard Tobin

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
 
R

Richard Bos

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
 
V

Vijay Kumar R Zanvar

Robert Clark said:

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/


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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)."
 
D

Dik T. Winter

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

Richard Bos

Dik T. Winter said:
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
 
R

Richard Tobin

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
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top