C – Why Are Both index[array] and array[index] Valid?

arrayclanguage-designpointersstandards

For example consider:

int index = 3;
int array[4] = {0, 1, 2, 3};

then both index[array] and array[index] are valid expressions, much like *(index + array) and *(array + index).

In C arrays why is this true? array[5] == 5[array] explains how it works. My question is: Why did the language designers choose to allow this? Why not just enforce that index[array] is invalid, for clarity's sake?

Best Answer

First of all, it would help to read dmr's Development of the C Language to get some insights into some of C's quirks, particularly when it comes to array semantics (basically, blame BCPL and B for most of it).

As for the question "[w]hy not just enforce that index[array] is invalid, for clarity's sake," what would such a check buy you in exchange for the cost of performing it? The form almost never appears outside of the IOCCC, so it's not like it's a major problem in production code (compared to the use of, say, gets, or unchecked array accesses (which disallowing i[a] won't help with), or <fill in the blank>). It's not a bug; it doesn't introduce any undefined behavior; it doesn't introduce any security holes not already present with a[i]; the only complaints against it are stylistic in nature.

It's like asking why both T *p and T* p are valid; there is no "why" beyond it being an accident of the language syntax. There's nothing deliberate behind allowing both, it's just a function of how the grammar works. Same with a[i] and i[a]. Professional programmers are (usually) grown-ups, and don't deliberately introduce confusion where it isn't warranted, so most will naturally use a[i].

You're basically trying to guard against a problem that doesn't really exist.

Related Topic