If you've been having trouble trying to do this, I'm assuming you were doing something like this:
pResult = p1 + p2
Which is definitely not what you want to do. The compiler probably wouldn't allow it anyway, but what that code would attempt to do is add the memory addresses of p1 and p2 together and assign pResult to the resultant memory address...which could contain literally anything!
But as I've said you'd more likely just get a compiler error saying that the two pointers cannot be added together like that!
Assuming you have three int pointers correctly assigned to valid int variables; To add the values of two pointers together you'd do this:
*pResult = *p1 + *p2;
Which simply adds the values stored at p1 and p2 together and stores the resultant value at pResult.
Well, I tried to get adding pointers to pointers working every which way I could and all but one ways resulted in a compile time failure.
int *p3 = *p1 + *p2; //Does NOT compile
In fact it spits out: error: invalid conversion from `int' to `int*'
The way I did get it to compile though is to do something like this:
using namespace std;
int a = 1;
int b = 2;
int *p1 = &a;
int *p2 = &b;
//Create the pointer and make it point to something
int *p3 = &c;
//Then assign the value of the pointers to the new pointer
*p3 = *p1 + *p2;
cout << *p3 << endl;
If there's some other way to do this then I'd love for someone to show me, but this is the only way that I could figure out on my own.
A pointer is supposed to point to a memory location. If you do something like: int* p3 = *p2 + *p1; then which memory location is it supposed to be pointing to? There is no memory location to point to in this example. So first, set up the pointer then add the values to the existing memory location.