I have detected a difference between VS2019 C++ and VS Code (mingw-w64) about pre post decrement with pointer usage. The following code in VS2019 shows prints the third element of array, 12 (a[2]):
A:
int a[] = { 10, 11, 12, 13, 14 };
int *p = a;
int *q = p + 3;
*q = a[(q--) - p];
cout << *q << endl;
If I change the post decrement operator as pre decrement:
B:
int a[] = { 10, 11, 12, 13, 14 };
int *p = a;
int *q = p + 3;
*q = a[(--q) - p];
cout << *q << endl;
it prints again 12 (a[2])
But if I run the code in VS Code with mingw-w64, then the first code block A: shows the fourth element of the array, 13 (a[3]) and the block B: shows the third element, 12 (a[2])
Why is VS2019 ignoring in this case the pre and post decrement and mingw-w64 not, if I use the same pointer on the left hand side of the assignment, which I use for indexing the array?
Do you have any experience/explanation about this behaviour?
Thank you
Onur
p.s It is an exam question, not a real case. Please don't ask me for a logical use of this code