What I Hate About Your Programming Language
Subject:   C, C++, Java, maybe others - zeroth element arrays
Date:   2003-05-15 02:07:51
From:   anonymous2
Response to: C, C++, Java, maybe others - zeroth element arrays

Perhaps you should move to COBOL :-), where the first element of an array is addressed as...... 1.
Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • C, C++, Java, maybe others - zeroth element arrays
    2003-09-18 13:18:15  anonymous2 [View]

    This is a bit late, but here are my $0.02.

    I like arrays in pascal with bounds from -n to +n: var a: array [-10..10] of integer; It is very natural, simple and with run-time bounds checking also very safe. With arrays like pascal's much of current buffer overrun exploits would (probably) not exist!
    • C, C++, Java, maybe others - zeroth element arrays
      2005-02-01 22:58:52  Nurchi [View]

      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.

      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