0

Why output of firstvalue is 10 not 20?

int firstvalue = 5, secondvalue = 15; int * p1, * p2; p1 = &firstvalue; p2 = &secondvalue; *p1 = 10; *p2 = *p1; p1 = p2; *p1 = 20;

21st Jun 2018, 9:06 AM
George Turdzeladze
George Turdzeladze - avatar
3 Answers
+ 1
Let's trace to see what's going on Q: Why output of firstvalue is 10 not 20? int firstvalue = 5, secondvalue = 15; int * p1, * p2; // p1 <- firstVal address p1 = &firstvalue; // p2 <- secondVal address p2 = &secondvalue; // indirect assignment // firstVal <- *p1 <- 10 *p1 = 10; // dereference and assignment (PAY ATTENTION) // secondVal <- *p2 <- firstVal <- *p1 *p2 = *p1; // pointer assignment (PAY ATTENTION) // address of firstVal = addrest of secondVal p1 = p2; // Indirect assignment // secondVal <- *p1 <- 20 *p1 = 20; firstVal = 10 secondVal = 20
21st Jun 2018, 10:40 AM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
21st Jun 2018, 10:36 AM
Billy Tsouvalas
0
dears thank you very much for useful feedback. It is clear now, just have a feeling of something "unusual". need to get used to it :)
21st Jun 2018, 10:57 AM
George Turdzeladze
George Turdzeladze - avatar