-1

I was going through C Pointer arithmetic. I found that pointer addition is not allowed but pointer + integer is allowed.

I thought pointer + pointer is not allowed due to security reason. But what if a pointer say p1 holds 66400 and p2 holds 66444. now p1+p2 is not allowed but p1+66444 is allowed. Why so?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Nitesh Kuhar
  • 55
  • 1
  • 8
  • Does pointer + pointer even make sense to you? What could the practical application of this possibly be? There is no application in adding the addresses of two objects in memory, while pointer + integer gives you an offset, which can indeed be useful. – dtech Jul 30 '15 at 09:44

5 Answers5

11

Think this way.

Address 1: 112, Bakers street
Address 2: 11, Nathan road

Why Address1 + 3 is fine but Address1 + Address2 is bad.

(BTW by Address1 + 3 I mean 115, Backers street)

For the same reason multiplication of scalar or an address to another address doesn't make sense.

address1 * 2 // Invalid
address1 * address2 // Invalid

Logically it is possibly to take an offset from an address by adding/subtracting but adding 2 addresses doesn't make sense because addresses of same variables may be different in each run of program. Moreover the type and value of addition don't make any sense.

I thought pointer + pointer is not allowed due to security reason.

No it is not allowed because addition of pointers does not make any sense.

if a pointer say p1 holds 66400 and p2 holds 66444. now p1+p2 is not allowed but p1+66444 is allowed.

You are thinking only about values think of their types also. For example if a holds 2 kg, and b holds 3 meter, it does not make sense to add them.


There is one more important thing to learn from address analogy:

Let's say there are 80 houses on Nathan road (analogous to arrays in C) and if you add 70 to Address 2 you may land in a house, a garbage bag, or in the sea. For the same reason, if you go more than 1 past the address in array or address before the of array behaviour is undefined. If you dereference any address beyond the array, behaviour is undefined.

int NathanRoad[80] = {...};
int *address1 = &NathanRoad[11];
int *q;
int s;
q = address1 + 3; /* OK */
s = *(address1 + 3); /* OK */
q = address1 + 75; /* Bad */
q = address1 + 69; /* OK */
s = *(address1 + 69); /* Bad */
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
2

Sure, you could treat pointers as integers, but you really shouldn't.

Pointer+integer means position+offset, which yields a new position. Pointer+pointer would mean position+position, which doesn't make sense.

Similarly in maths, if that helps you. While a 2D vector and a 2D coordinate can both be expressed similarly, it only makes sense to add the vectors, not the coordinates.

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
2

Just think of it logically.

If you add two pointers, what will be the result? It is most likely to make no sense.

OTOH, addition of an integer means increment (or decrement) the pointer location (with regards to the type of pointer). The pointer is the location and the integer is the offset. Logical and valid.

Technically speaking, the addition + operator, from C11, chapter §6.5.6, also mandates,

For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type. (Incrementing is equivalent to adding 1.)

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Pointers aren't integers. Pointer arithmetic works like this: If you have a pointer p to an array element a[i] and and integer n, then p + n is a pointer to a[i + n]. That's it.

melpomene
  • 84,125
  • 8
  • 85
  • 148
0

Adding pointer to pointer is pointless (pun intended) it just doesn't make sense.

Adding a pointer to an integer can give a useful result. Depending on the usage of course.

Subtraction of two pointers is supported because it can be useful for estimating sizes. How many elements would fit in between?

Jose Luis
  • 3,307
  • 3
  • 36
  • 53