> C/C++ adding pointers?

C/C++ adding pointers?

Posted at: 2014-12-18 
is is possible to add two pointers in this manner?

int addPointers (int *a, int *b)

{

return *a+*b;

}

void main(void)

{

int s = addPointers(9,9);

}

Not even close. First of all, your function takes two pointers to an integer but you are passing two plain integers (9,9). The compiler understands 9 to be an integer, and an integer will not be understood to be a memory address. You have to use "&" to indicate a memory address, and that won't work with a constant literal like 9.

Also, if you are passing in two pointers, and then you add them together, they are still of type pointer, and if you want to return the sum then the return type would have to be pointer also ( int* foo(int*, int*) ); and s would have to be a pointer too.

So, ....... back to the drawing board.

is is possible to add two pointers in this manner?

int addPointers (int *a, int *b)

{

return *a+*b;

}

void main(void)

{

int s = addPointers(9,9);

}