+ 1

Value is not incrementing

In the following code the values of i is not incrementing. I am unable to understand such kind of behavior. #include <iostream> using namespace std; int main() { int i=5,*j; j=&i; *j++; cout<<i<<" "<<j<<endl; ++*j; cout<<i<<" "<<j<<endl; return 0; }

21st Feb 2018, 8:40 AM
Samir Imtiaz
Samir Imtiaz - avatar
3 Answers
+ 4
It’s because of operators precedence, see the table here: http://en.cppreference.com/w/cpp/language/operator_precedence ++ has a higher precedence than dereferencing *
21st Feb 2018, 9:07 AM
Sergiu Panaite
Sergiu Panaite - avatar
0
I understand ,but the second "++*j" is preincrement and should increment value of "i".so the final value of i should be 7 .
21st Feb 2018, 9:24 AM
Samir Imtiaz
Samir Imtiaz - avatar