So, I'm stuck at pointers. If anyone can help me understand this code I would be pleased.
This is one code devided in two parts.
CODE
#include <iostream>
int main(void)
{
int firstvalue, secondvalue = 12;
int * p1, * p2;
p1 = &firstvalue;
p2 = &secondvalue;
*p1 = 10;
*p2 = *p1;
I understand until this line, next line I don't understand. If I comment
p1 = p2 everything works as expected. Well as I expect it. I expect firstvalue to be 20, and secondvalue to be 10.
CODE
p1 = p2;
*p1 = 20;
std::cout << "firstvalue is " << firstvalue << std::endl;
std::cout << "secondvalue is " << secondvalue << std::endl;
getchar();
return 0;
}
Output (without comments) is:
firstvalue is 10
secondvalue is 20
How can that be? The last command is
*p1 = 20. Firstvalue should be 20, shouldn't it? *sighs*
*Topic title edit*
This post has been edited by pietra: 23 Mar, 2007 - 02:22 AM