To reiterate: Why does array_name[index] == index[array_name] in C++?
I am struggling to find anything online that explains why you can reference an element like this in C++. I have a java background and do not understand the nuances of C++ that well.
Here is an example illustrating the question:
#include <iostream>
using namespace std;
int main()
{
int data[3] = {1, 2, 3};
if (data[2] == 2[data])
{
cout << "equal" << endl;
cout << data[2] << ", " << 2[data] << endl;
}
else
{
cout << "not equal" << endl;
}
}
Output:
equal
3, 3