2

Why is GCC giving me this error ? What am I doing wrong here ?

 temp.c: In function main:
 temp.c:6: error: invalid operands to binary +

Code:

 main()
 {
     char *Address1,*Address2,*NewAddress;
     Address1= (char*)0x12;
     Address2= (char*)0x34;
     NewAddress = Address1+Address2;
 }
Jean
  • 21,665
  • 24
  • 69
  • 119
  • You can't add pointers. Why would you ever want to? It _does_ make sense to add a pointer and an offset, but not two pointers... – Mooing Duck Dec 20 '11 at 17:32

6 Answers6

6

Why do you want to do that?

The C language forbids addition of two pointers. It only defines (with severe restrictions) the addition of a pointer and an integer.

Basically, you can only add to a pointer an integer small enough so that the result was inside, or at the ending border, of some allocated memory zone.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I thought its up to the user to make sure that addresses wont peep out of allocated space. Why is C imposing this restriction ? – Jean Dec 20 '11 at 17:47
  • The C language standard says -in more politically correct words- that if a pointer "points nowhere", the behavior is undefined. And undefined behavior means really anything: exploding the computer is a valid implementation of undefined behavior. – Basile Starynkevitch Dec 20 '11 at 18:00
5

C does not permit adding two pointers. The best explanation I found for this, not touching any standard, is Rouben Rostamian's quote:

If you and I live on the same street, the difference of our house numbers is some sort of indication of the distance between our houses.

Now, you tell me, what meaning is there in the sum of our house numbers?

So the whole operation does not make any sense. It is pretty logical, isn't it? You can do this, however:

NewAddress = (char *)(0x12 + 0x34);
0

You can't add pointers together, that would be nonsensical.

What is allowed is adding integral values to pointers:

char *Address1,*NewAddress;
unsigned Offest;
     Address1= (char*)0x12;
     Offset= 0x34;
     NewAddress = Address1+Offset
Pubby
  • 51,882
  • 13
  • 139
  • 180
0

Because those variables are pointers, try convert to int.

NewAddress = (char *)((int)Address1 + (int)Address2);
designerrr
  • 222
  • 1
  • 2
  • 8
0

In C, you can't add two pointers. If you think about it, it doesn't logically make any sense to try. To fix this, you can cast one of the pointers to an integral value before adding:

NewAddress = (long)Address1 + Address2;
Dan Fego
  • 13,644
  • 6
  • 48
  • 59
-4

These answers are awful. I accept that recent C standards may deprecate the feature, but there are valid reasons to add and subtract pointers, e.g. when one is relative or you want to create a relative address. C was always intended to be a high level assembler and relative addressing is a fundamental OP in all modern CPUs.

As to the proposed solutions: what is VERY bad practice is to cast each pointer to integer and then subtract, since a pointer may not fit into an integer. However (INT)(p2-p1) ought to be safe, assuming the P1 and P2 addresses are within the same context, i.e. known to be in the same buffer.

  • 1
    There are no relative addresses stored in pointers in C. If you want an offset, you can add an integer to a pointer. – Gerhardh Sep 16 '20 at 11:10