Backward array index always had a use in the code golf context, like this:<p><pre><code> int a[3] = {0, 1, 2};
int *p = a;
int **q = &p;
int x = (*q)[1]; // Read a[1]
int y = 1[*q]; // Same, but saves 2 bytes
</code></pre>
Code golf considerations aren't always related to practicality, of course.
I’ve always thought of the array index operator:<p><pre><code> a[index]
</code></pre>
as syntactic sugar for the pointer arithmetic:<p><pre><code> *(a + index)
</code></pre>
From this point of view, the existence of a “backwards” index operator makes sense; the arithmetic evaluates to the same address.
> Astound your friends! Confuse your enemies!<p>... more like your friends will curse you and your enemies will watch with glee that you are using C++
What this is talking about is the behavior of [] in C and C++. In these<p><pre><code> x[y]
</code></pre>
and<p><pre><code> y[x]
</code></pre>
are the same. I was first introduced to this by a friend of mine many many years ago (because I'm an old) as<p><pre><code> for (i = 0; etc)
putc(i["hello world"])
</code></pre>
or similar nonsense.<p>The C++ change that makes this matter is that apparently pre c++17 doesn't enforce sequencing such that expression1[expression2] doesn't require expression1 be evaluated before expression2. C++17 does actually fix the sequencing to be left to right, so now expression1[expression2] will always evaluate as expression1;expression2 and expression2[expression1] will always evaluated as expression2;expression1 without depending on UB.
Oh C++… The level of excitement over pointless triviality you generate never ceases to amaze me.<p>Some nerds somewhere are getting all giddy about this silliness when it would just be objectively better to not have this silly quirk in the first place and to write it like a sane person who recognizes the great social benefits of maximizing understanding:<p>auto idx = index();
return p[idx];<p>Clever generally just means “bad”. Why people get so excited about it mystifies me…
Before reading the article, I thought he was talking about negative indices.<p>I was reading K&R the other day and spotted the bit where they mention c supports negative indices and gave an example. My mind was blown.