Can I change the way index values work?

Whenever we count the number of something in real life we begin with 1 because it would be silly for us to start with 0, meaning nothing, and then advance to one. It seems even sillier to assign the first element of an array an index value of 0 because the array isn't empty- it has at least one element.

I know how numbers are represented as bit patterns in computer hardware and the bit patterns begin with zero but the source of this dilemma seems to originate with the language standard rather than with any hardware constraints.

Is there any way I can modify the c++ language rules or configure Visual Studio 2015 to automatically -1 from all index values at compile time?
The "source of this dilemma" is that C++ is evolved from C. C was designed to be very close to the corresponding machine instructions in order to be very fast and efficient in execution.

If one considers an array as simply a block of memory where we have a pointer to the start of that block, then the first element is located at position (start+0), the second element at position (start+1) and so on. Thus zero-based arrays make sense at the machine level.

Can the rules be modified? Not really. You could write your own class or wrapper to have a user-defined array which operated the way you'd like, but that would end up being even more confusing as it would work differently to common structures such as std::string. Better to simply practice and become familiar with using the existing standard - or maybe use some other language instead. I recall an old version of BASIC which allowed arrays to start at index 1 (as an option). Not sure about other current languages.
Other computer languages:

Fortran (at least, modern versions) allow you to start arrays with whatever you like (as long as you declare it). This includes 0 and 1, but 1 is the default and always has been.

Visual Basic allows you to start arrays with whatever you like (as long as you declare it). This includes 0 and 1; 0 is now the default - but I think it may previously have been 1 (which must have been an interesting change!). I think that VB.net enforces the same 0-based array start as C/C++.

I can see arguments for both, but I tend to side with starting at 0 for the same reason as @Chervil cites, and it comes down to having a pointer to the start of the array with subsequent array indices then being the same as the number of increments of the pointer.

As I have a lot of code which has both Fortran and C/C++ components I definitely have to be aware of the issue during function/subroutine calls. Believe me, array start number is nothing like as problematic as two other big differences: Fortran routines pass arguments by reference by default, whereas C/C++ pass by value; worse still is the ordering of multi-dimensional arrays - in Fortran, memory storage has the leftmost index varying fastest; in C++ it is the reverse.

In terms of your original question - there is nothing stopping you from declaring your arrays 1 element bigger than they need to be and simply ignoring the [0] element. However, I suspect that subsequent use of pointers gets more tricky.
there is nothing stopping you from declaring your arrays 1 element bigger than they need to be and simply ignoring the [0] element.

Indeed, I sometimes do this. A specific example is converting month numbers to month names,
1
2
3
4
5
    const string mnthNames[13] = { "",
        "January", "February", "March", "April",
        "May", "June", "July", "August",
        "September", "October", "November", "December"
    }; 

This allows easy conversion from an ordinary date in number format to text format.

Regarding starting from index zero, a strong motivation for becoming familiar with this way of doing things is that it makes reading code written by other people much easier. Getting to grips with someone else's code can sometimes be a challenge, even in the most well-written code, and using the same conventions as others at least removes one unnecessary obstacle in that path.
Last edited on
I found this on the Microsoft msdn web site - it makes me so glad that I don't have to code in Visual Basic!

For an example of a one-based Visual Basic function, consider the Mid function. It takes an argument that indicates the character position at which the substring will start, starting with position 1. The .NET Framework String.Substring method takes an index of the character in the string at which the substring is to start, starting with position 0. Thus, if you have a string "ABCDE", the individual characters are numbered 1,2,3,4,5 for use with the Mid function, but 0,1,2,3,4 for use with the String.Substring method.
Topic archived. No new replies allowed.