How the value of m is 2?

58 views Asked by At

How the value of m is 2 after execution as i is postfix ?

int main()
{
   int i,m;
   int a[5]={8,10,1,14,16};
   i=++a[2];
   m=a[i++];
   cout<<i<<m;
   
   return 0;
}
1

There are 1 answers

0
iammilind On

Your code can be broken down to following statements:

auto& temp = a[2]; // temp refers to 1
temp = temp + 1; // temp aka a[2] is now changed to 2
i = temp; // i = 2
m = a[i]; // *** m = 2 ***
i = i + 1; // i = 3

This is how m = 2 happens!