In C the expression array[n] is actually just syntactic sugar for
*(array + n). Literally. I haven't tried in years, but the following
is supposed to be legitimate: n[array]. Great for obfuscating code.
This indeed works, and consequently funny variations such as
(n1-n2)[n2+array] work too. It is obfuscating not so much because
it were not easy to understand, but because we simply are not used to=20
see it written that way (because other languages usually do it in=20
the other way around).
To contrast with, the IMO most unpleasant way of approaching the index=20
origin was done in APL. There, you could set the system variable _IO=20
to either 0 or 1 (no other value accepted), which rules whether array=20
the index of *any* array is from now on counted from 0 or from 1.=20
True, you *could* localize this setting in a function, but at least=20
in original APL (I don't know about APL2), these were not truly
"local" variables like in Ruby, but more like those "local global"=20
variables you would for example declare in Perl by writing=20
'local $v=3D....;', with the consequence that they the change to _IO
would be visible in all functions called from within the function
which had "locally" reset the index origin. This has the consequence
that if you write *any* reusable function for APL (such as a library),=20
you must use the value of this variable for indexing. For example,
in order to access the Nth element of an array A, you would write
something like A[N+_IO-1].
BTW: Though I - like most programmers - find it most convenient counting
arrays from zero, there are cases where I would find it more natural
if I could choose the upper and lower bound of an array by myself (as
it is done in languages such as Ada). Such an approach would work well
in a statically typed language (where you can in principle look up
the variable declaration), but would make understanding a program=20
unnecessarily hard in dynamic languages such as Ruby.
Ronald