ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  What I Hate About Your Programming Language
Subject:   C, C++, Java, maybe others - zeroth element arrays
Date:   2005-02-01 22:58:52
From:   Nurchi
Response to: C, C++, Java, maybe others - zeroth element arrays

As people mentioned before, Pascal had the best implementation for the indeces of the arrays, when you can specify the lower and upper limit of the array.
But as we all know (well, most of us) the 0-based arrays go many years back, when C was created (actually, way before that).
As we know, any array in C/C++ (C style array, not <Vector>) is just a pointer to the first element of the array, so declaration:
int[] a;
is equivalent to:
int* b;


The number in square brackets indicates offset from the first element, so
"a[0]" means the same as "*b" and "a[5]" means the same as "(*(b+5))"
It was made this way, some people think it is stupid, but talking about all the stupid things that happen in our world, let's blame Hitler for..., but would anything change?
Just understand the concept and be consistent with it. I think it is kind of cool to use
for (int i=0; i {
//Do something here
}


The idea is that you go from 0 to 'i' strictly less than whatever, and as long as you are consistent with it, ...
I think, the bounds of the arrays are fixed and there is nothing we can do, and I am used to it, and I personally (some of you might not agree, but I don't care) think, it should be this way now. It is like this, and it shouldn't be changed.


AFAIK (As Far As I Know) even VB.NET has 0-based arrays now, starting from Visual Studio .NET 2002 (correct me if I am wrong).


Anyway, I think, discussions are OK, but when people argue about what is better, and what is worse, it is just stupid.
I (now) prefer 0-based arrays, and it would be hard for me (at least for the first time) to switch back to 1-based...
Everyone should be good at something. If you're good at 1-based, there you go, use whatever programming language you want to use, or just create the array 1 element larger than you actually need it. That way you waste some memory on element with index 0, but you for sure can access elements from 1 up to (and including) MyArray.Length


OR MAKE YOUR OWN DAMN COMPILER!!!!!!!!!!!!!!!


Good luck.


P.S.
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do,
it blows away your whole leg."
--Bjarne Stroustrup