Arré
Arré is an expression one would occasionally come across if they heard somebody from India speak English. It is a kind of sub-Himalayan expression for “Oh!” and it finds its way in one sentence or another, just like “Oh!” does, to indicate a range of reactions like surprise, annoyance, anger, or even something else, depending on the context.
In computer programming, however, its homophone “array” is used to indicate something entirely different, far from just an exclamation - it is an ordered collection of “things” that can be accessed using an index number, starting from 0. Take integers as an example - {0, 1, 2} is an array of three integers.
For years I’d just thought about about arrays like that - which is conceptually not incorrect - but I’d never bothered to figure out what exactly their syntax meant/represented in certain programming languages. For instance, in C or C++, an array can be defined as:
int arr[] = {0, 1, 2};
It makes sense to think that arr
is the array variable name for an array having those three elements. With a bit of understanding about pointers, we can gather that arr is actually an int*
(integer pointer) pointing to the first element of the array.
Thus, based on an understanding of dereferencing logic it can be inferred that *arr
== arr[0]
, *(arr + 1)
== arr[1]
, and so on, since arr
just indicates a memory address pointing to the first element, and subsequent increments point to the elements that follow in memory.
Simple enough, and quite logical.
But something about this equivalence really fascinated me - the realization that x[y]
is really just syntactic sugar for *(x + y)
- and hence arr[1]
is really *(arr + 1)
, which is == *(1 + arr)
, and therefore arr[1]
can also be written as 1[arr]
! Never had I thought an expression like 1[arr]
could make sense, least of all be compilable code.
In all these years I’d never thought about it like that but seeing the following code compile got me quite excited with childlike wonder:
It is of no use knowing this from a practical standpoint, but the fact that such syntax is legitimate and compilable just amazes me. Some very fundamental questions on Stack Overflow can yield really interesting pieces of information and change how one perceives taken-for-granted stuff like programming syntax.
This may sound a bit trivial to computer programmers who’ve been in the field for years and know all of this already, but this thing about arrays and specifically the seemingly strange syntax led to an arré moment for me! I can’t quite figure out exactly why but I suspect (w.r.t. the code above) that it’s because of what nonsensically appears to be accessing an array element by indexing a constant value is really dereferencing the pointer at the address given by the sum of 0
and tstString
.
Everything to do with programming is just syntactic sugar beyond a certain point of abstraction (after all, it is just machine code jumping electric charges that programs eventually end up becoming, following the code compilation wizardry)!
Essentially, it kind of shows that when you interpret things with a little bit of additional information, their meaning can change completely - or in this case, what seems to be apparent nonsense is actually meaningful and perfectly sound, even if unconventional.
Talk about the general applicability of this idea to everything around us! Or perhaps on the flip side, not quite.
Arré enough for me.