Return pointer value errors

the value of x and y in the returning pointers is not as expected. X value is right but y value is wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;
struct point
{
    void setPointValue();
    void print();
    double x,y;
};
point* add_2d_points(point*,point*);
int main()
{
    point p1,p2,p_additive;
    point *p1_ptr,*p_additive_ptr;

    p1_ptr=&p1;
    p1_ptr->setPointValue();

    point *p2_ptr=&p2;
    p2_ptr->setPointValue();

    p1_ptr->print();
    p2_ptr->print();

    p_additive_ptr=add_2d_points(p1_ptr,p2_ptr);
    p_additive_ptr->print();              //the problem appears here
    return 0;
}
point* add_2d_points(point* a,point* b)
{
    point p;
    point*p_ptr=&p;
    p_ptr->x=a->x+b->x;
    p_ptr->y=a->y+b->y;
    return p_ptr;
}

void point::setPointValue()
{
    cout<<"Entre value of x\n";
    cin>>x;
    cout<<"Entre value of y\n";
    cin>>y;
}

void point::print()
{
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl;
}
Line 30: p is a local variable. it goes out of scope when add_2d_points exits.
Line 34: p_ptr points to something that no longer exists.
thanks
Topic archived. No new replies allowed.