Hi i am wondering if its possible to do the following,say i have 2 integer pointers containing numbers,and i want to add them together to a third integer pointer,is this possible?

>is this possible?
Is this a troll? Or a bad joke? Of course it's possible, as are most things that noobs use the "is it possible?" question to ask about.

int a = 1;
int b = 2;
int *p1 = &a;
int *p2 = &b;
int *p3 = *p1 + *p2;

Yes it is possible.

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.

Cheers for now,
Jas.

Thanks for the replies,is this because *p1 is derefrenced here so contents can be added?

Thanks for the replies,is this because *p1 is derefrenced here so contents can be added?

Yep, just think about it like this, if ptr is a pointer-to-int, then *ptr,
is the value that it points to , so if it points to an address, which
has the value 4, then *ptr == 4 is true.

int a = 4;
int * p = &a;

int t = *p; // same effect  as int t = 4;

i have a doubt regarding this....

int *pa =4

is considered unsafe isn't it?? so how does

int *p3=*p1+*p2

work properly...
ty in advance

i have a doubt regarding this....

int *pa =4

is considered unsafe isn't it?? so how does

int *p3=*p1+*p2

work properly...
ty in advance

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:

#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    int c;
    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;

    return 0;
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.