0

Pointer and values

After posting a question about pointers, I came across another example, which is not clear to me after some lines, which I indicated in the code.They equalize the parameter and the pointers, but the end values are different.Why? https://code.sololearn.com/c22ew0h34673/?ref=app

18th Jun 2017, 3:53 PM
Anil Yilmaz
Anil Yilmaz - avatar
10 Respuestas
0
the answer is 10 and 20.
18th Jun 2017, 4:17 PM
Anil Yilmaz
Anil Yilmaz - avatar
0
Just imagine it like people: We have person firstvalue, he is holding a number, 5 person secondvalue is holding number 15. Next we have 2 other people, p1 and p2. p1 = &firstvalue = p1 is pointing towards firstvalue and p2 = &secondvalue = p2 is pointing towards secondvalue Whenever we use * we are talking about the person that is being pointed at. Without the * we are talking about the person who is pointing. *p1 = 10 = The person p1 points to (firstvalue) changes his value to 10. firstvalue now holds 10, secondvalue still holds 15. *p2 = *p1 = The person p2 is pointing to (secondvalue) changes his value to the number p1 is pointing to (firstvalue), so secondvalue becomes 10. Now firstvalue holds 10, secondvalue holds 10. p1 = p2 = The person p1 now copies whatever person p2 is doing. p1 is now pointing to secondvalue (note that no one is pointing to person firstvalue now). firstvalue is still holding 10, secondvalue is also still holding 10. *p1 = 20 = The person p1 is pointing to (secondvalue) now changes his number to 20. Final result: firstvalue = 10 secondvalue = 20
18th Jun 2017, 4:28 PM
Dennis
Dennis - avatar
0
Thanks a lot. Is it necessary to write p1=p2 after the pointers are equal. The idea of copying the value by equal sign is new to me.Equal sign assigns the left hand side to the left hand size.Now we say, it copies. I guess, I should consider the pointer with its variable.P1=P2 means P1 pointer adresses the variable that P2 indicates.Or ir has the variable adress of second variable.
18th Jun 2017, 4:35 PM
Anil Yilmaz
Anil Yilmaz - avatar
0
Yes, the '=' sign copies. What they(p1 and p2) are pointing towards may both hold the same value, 10 in this case, p1 is still pointing towards another person than p2 does. The pointers themselves, which is what we are talking about here, are NOT equal because they point to different people. p1 = p2; Makes them point to the same person, I'm saying copying because p1 copies the address that p2 points to. This address is all that gets copied.
18th Jun 2017, 4:50 PM
Dennis
Dennis - avatar
0
Dennis, can you please see my other post? int &y= x means y=x.That was what an answer says.Thanks a lot.
18th Jun 2017, 5:15 PM
Anil Yilmaz
Anil Yilmaz - avatar
0
"int &y = x means y=x" Is not entirely correct, &y behaves similar like a pointer. That means y will copy the address of x, not its value. Whenever you say y = 5 or x = 5 the value at address of x will change, not y.
18th Jun 2017, 5:20 PM
Dennis
Dennis - avatar
0
Here I see something different.&y is not a pointer, but an integer? How can it be? https://code.sololearn.com/cGWfWrWRPMe3/?ref=app
18th Jun 2017, 6:19 PM
Anil Yilmaz
Anil Yilmaz - avatar
0
y is an integer initialized with an address. x and y will share the same value in memory. x==y will always be true
18th Jun 2017, 6:29 PM
Louis
Louis - avatar
0
Like I said, & behaves similar to a pointer. There are some differences though. You don't dereference them with * and you don't use the -> operator with it and a reference must be initialized when declared and cannot point to something else. A reference is just another name for whatever it is referencing. For int &y = x; Whenever you call for y it is the same as calling for x. So thats why you don't use *y. (I'm assuming that's what you mean)
18th Jun 2017, 6:32 PM
Dennis
Dennis - avatar
0
This topic is not covered here.I only saw pointer type usage, not this way.Thanks a lot.
18th Jun 2017, 6:32 PM
Anil Yilmaz
Anil Yilmaz - avatar