1. ## New to pointers

Hi, I sort of understood pointers, until I went to cplusplus.com and saw this code:

Code:
```#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;
}```
the output is:

firstvalue is 10
secondvalue is 20

Ok, what I dont understand is why first value is 10 and not 20. I would really appreciate any simple explanation.

Oh and...Is there something I could think about or remember so that I dont get confused with pointers?

THANKS

2. The value of firstvalue is changed via p1. p1 is not a copy; it actually points to the value.

3. Code:
```#include <iostream>
using namespace std;

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

cout << "begiinging" << endl;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;

p1 = &firstvalue;  // p1 = address of firstvalue
p2 = &secondvalue; // p2 = address of secondvalue

cout << "after pointer assignments (nothing)" << endl;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;

*p1 = 10;          // value pointed by p1 = 10

cout << "firstvalue changes" << endl;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;

*p2 = *p1;         // value pointed by p2 = value pointed by p1

cout << "secondvalue changes" << endl;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;

p1 = p2;           // p1 = p2 (value of pointer is copied)

cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;

*p1 = 20;          // value pointed by p1 = 20

cout << "secondvalue changes" << endl;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;

return 0;
}```
Originally Posted by Tesnik
Oh and...Is there something I could think about or remember so that I dont get confused with pointers?
bunnies
THANKS
you're welcome

4. I dont see why *p1 = 20 makes second value change. p1 is the reference to the firstvalue, so why does secondvalue change and not the firstvalue.

ive tried with the bunnies... they dont work.

5. For now you have this:

Code:
```p1 ----> firstvalue
p2 -----> secondvalue```
Code:
`p1 = p2;           // p1 = p2 (value of pointer is copied)`
Now you have this:

Code:
```firstvalue

p1 --------------> secondvalue <------------- p2```
The first one makes p1 equal to p2. This means they both point to secondvalue.

Code:
`*p1 = 20;          // value pointed by p1 = 20`
Now you take whatever p1 points to, which is secondvalue, and change it to 20.

Make sense now?

6. yes!

thanks very much