I'm learning C++ and I have a question about the tutorial on pointers

// more pointers
#include <iostream>
using namespace std;

int main ()
{
int firstvalue = 5, secondvalue = 15;
int * p1, * p2;

p1 = &firstvalue; // p1 = address of firstvalue
p2 = &secondvalue; // p2 = address of secondvalue
*p1 = 10; // value pointed by p1 = 10
*p2 = *p1; // value pointed by p2 = value pointed by p1
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // value pointed by p1 = 20

cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}

I was wondering if since p1 = p2 and then *p1 = 20 if the code is retroactive and the *p1 = 20 makes p2 = 20. Thank you for your help
Try it and see.

put a cout in there and see if it changes.
I want to correct a few comments:

*p2 = *p1; // value pointed by p2 = value pointed by p1
to
*p2 = *p1; // value pointed by p2 now contains the value pointed by p1

p1 = p2; // p1 = p2 (value of pointer is copied)
to
p1 = p2; // p1 points to the same lcoation as p2.

I was wondering if since p1 = p2 and then *p1 = 20 if the code is retroactive and the *p1 = 20 makes p2 = 20.

Yes, after those two lines, they both point to the same value, which after that is changed to 20, so they both point to the same value, 20.
Okay, that solves my question! Thank you very much for your help :D
Topic archived. No new replies allowed.