+ 3

Question about pointer and reference outputs

Why is the output to the following code 1021 and not 1121? int x= 10, y= 20; int *ptr = &x; int &ref = y; *ptr++; ref++; cout<<x<<y;

7th Sep 2018, 1:41 AM
Philip
Philip - avatar
4 Respuestas
+ 3
No, it was not right. It is due to operators priority. ptr is increased before reading. Try (*ptr)++.
7th Sep 2018, 2:08 AM
Sergey Ushakov
Sergey Ushakov - avatar
+ 2
Philip I wasn't right and corrected my answer.
7th Sep 2018, 2:21 AM
Sergey Ushakov
Sergey Ushakov - avatar
+ 2
Thank you, Sergey Ushakov!
7th Sep 2018, 2:22 AM
Philip
Philip - avatar
+ 1
Tind 😉
7th Sep 2018, 6:09 PM
Angelo Sorte
Angelo Sorte - avatar