Keith Thompson wrote:
> And we wonder why people think comp.lang.c is a hostile newsgroup.
Not to support rudeness ...
> There are applications in which 1-based arrays are more convenient
> than 0-based arrays.
Or disagree with this
> 1-based arrays
> are much more convenient for some mathematical algorithms (see
> _Numerical Recipes in C_, which I cited upthread).
But this is a bad argument. I am initimately familiar with the book
.... the use of 1 based arrays was for ease of translation from
algorithms that had been coded in Fortran in previous versions of the
book, and the exact implementation invoked undefined behaviour (as you
mentioned in passing). The original Fortran coded examples were
actually useful, they were idiomatic for the language. The C examples,
when that came out, were completely unidiomatic and difficult to
follow; but a limited amount of effort had been put into it making it
useable. The Fortran 90 version, as far as I and my friends can see,
are actually almost unuseable (and I haven't looked for any more modern
versions
Yes not letting the arrays be anything other than 0 based is a weakness
of C. There are, however, very few algorithms where the structure of
the indexing set matters nontrivially, and even in those cases it is
usually the order relation that is important. In the few cases that it
does matter, sets of the form {0...b^n-1} (I am using ^ for `to the
power') are by far the most common.
I am not saying that one does not think of the first or second rather
than zeroth element: one certainly does. But that is a trivial change
during i/o or converting some field in a record. What I am saying that
when one needs to do arithmetic with the index, it is usually (but not
always) the zero based index that one wants. (Ask me! Having
programmed in various versions of Fortran now for well over twenty
years, I know how happy I was when I could use zero based arrays in
Fortran. And I also know how little it really matters in real life
when one gets used to using the `wrong' offset.)
In fact next to the indexing set {0...b^n-1}, the one I would like most
is not {1...b^n-1}, but rather {-b^n+1...b^n-1}. 1 based arrays is,
and I speak from experience, is not really a big deal.