0
int main()
{
  int x=10;
  cout<<"value of x: "<<x<<endl;
  cout<<"address of x: "<<&x<<endl;

  int *p;
  p=&x;
  cout<<"value of p: "<<p<<endl;
  cout<<"address of p: "<<&p<<endl;
  cout<<"pointer of p: "<<*p<<endl;

  cout<<"\n"<<"ADDING"<<"\n\n";
  p=p+2;
  cout<<"value of p: "<<p<<endl;
  cout<<"address of p: "<<&p<<endl;
  cout<<"pointer of p: "<<*p<<endl;

  return 0;
}

In the above code i am adding a constant number to a pointer p,but if i try to add 2 pointers then i get an error that it is not allowed.

The reason that i understood of not adding 2 pointers is that it could result in a overflow and crash the code.

But if i wanted to crash the code then i could have added a larger number to the pointer like below:

int main()
{
  int x=10;
  cout<<"value of x: "<<x<<endl;
  cout<<"address of x: "<<&x<<endl;

  int *p;
  p=&x;
  cout<<"value of p: "<<p<<endl;
  cout<<"address of p: "<<&p<<endl;
  cout<<"pointer of p: "<<*p<<endl;

  cout<<"\n"<<"ADDING"<<"\n\n";
  p=p+10000000000000000;
  cout<<"value of p: "<<p<<endl;
  cout<<"address of p: "<<&p<<endl;
  cout<<"pointer of p: "<<*p<<endl;

  return 0;
}

Thus the above code would crash, if crashing was the reason then why are we allowed to even add a constant to a pointer.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
Hitesh
  • 157
  • 3
  • 14

4 Answers4

2

The reason that i understood of not adding 2 pointers is that it could result in a overflow and crash the code.


That is not the only reason. The main reason is that it does not make any sense in a pointers perspective.

Think when you are adding a constant number like 2 to p. That would mean that you want p to skip and element and start pointing to the 2nd element in the linear memory block.

But since adding 2 pointers makes no sense, it is not useful at all.


Quoting K&R,

The valid pointer operations are assignment of pointers of the same type, adding and subtracting a pointer and an integer, subtracting or comparing two pointers to the members of the same array, and assigning or comparing to zero. All other pointer arithmetic is illegal. It is not legal to add two pointers...

Haris
  • 12,120
  • 6
  • 43
  • 70
1

The reason that i understood of not adding 2 pointers is that it could result in a overflow and crash the code.

That is not correct.

The binary + operation, and its meaning, is defined by the standard for a pointer and an integral type.

The standard simply does not define the binary + operation between two pointers.

This is not the right place to discuss why the standard does not define that operation.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You are not adding two pointers. p holds the address of x. So once you add P+2 then pointer is pointing to +2 address location Example if your x is at 0x1000 address location then after adding, your pointer will point to (0x1000+(2*sizeof(int))) address location

Aditya B G
  • 21
  • 3
0

Adding constants to pointers can be useful, such as for traversing a data structure where elements are contiguous in memory.

However, it is harder to find a use case for adding two pointers, as that would give an essentially irrelevant address which, as you say, could be illegal.