Normally we access an array element in this manner: arrayName[elementID]
. But even we use like elementID[arrayName]
it compiles and does not cause any errors in runtime. Isn't it logically wrong? Can anyone explain me this. I'm new to C++. Thank you in advance for any help!
#include<iostream>
using namespace std;
int main()
{
int arr[4] = {2, 4, 5, 7};
cout << arr[2] << endl; //this is the correct way to use it
cout << 2[arr] << endl; //this gives the same result and does not cause any errors
return 0;
}