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;
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
0
Check this out.
https://code.sololearn.com/c40AiP26AbWG
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 :)