views:

2558

answers:

24

Duplicate of:

Defend zero-based arrays

Why did the computer programming inventors have the brilliant idea of making arrays start with index zero and leave us all doing myArray.length()-1 when they knew that the logical thing would be to have the index start from 1?

Is it some sort of backwards compatibility due to which all (even modern) languages have arrays start from 0, or is there actually any logic behind this?

+7  A: 

I disagree that it's not logical...

it makes constructs like

for (int i = 0; i < array.Length; i++)

prettier than the alternative

for (int i = 1; i <= array.Length; i++)

Also, in the old days of C (and C++)

*(array + offset) is equivalent to array[offset] for zero-based arrays.

for one-based arrays, this would be

*(array + offset) -> array[offset + 1]

Andrew Rollings
Still, it would be more intuitive to write (i <= array.length()), also other than loops there are a 1000 other things where you have to write length()-1 to get the correct number of elements in an array
Click Upvote
array.length() will give the correct number of elements in the array. Its just that the number isn't the index of the last element...
Arjan Einbu
The correct number of elements in an array *is* length.
Andrew Rollings
Yes, sorry, i meant the index of the last element. My bad
Click Upvote
+7  A: 

Because each byte consists of a series of 0 and 1's. The lowest (non negative) number representable is 0. So why waste a perfectly valid number.

But besides that, there are languages that let you start an array at 1, or 13 or 42, whichever you like, It really does not make a difference.

The problem is that it is commonly accepted that we start counting at 0.

Gamecat
Um, because programming languages are for humans, and humans' common sense would say the first (1st) element of an array should be at 1?
Click Upvote
I dunno about you, but I start counting at 0.
Andrew Rollings
what are you talking about, programming may be for humans but everytime you have to deal with binary your set to go from 0000 to 1111 no need to abstract that
DFectuoso
Memory and processing speeds weren't always so cheap.
Spencer Ruport
+48  A: 

Specifically to annoy you.

Spencer Ruport
hehehe , please make this wiki so I can upvote it... jejeje good answer.
OscarRyz
Forget the wiki a good answer deserves rep.
Unkwntech
Funny, but not helpful. :) +0
Andrew Rollings
I just got the feeling it was the answer he was expecting.
Spencer Ruport
to annoy all of us, not just me
Click Upvote
nope - just you :)
seanb
A sarcastic non-technical answer for a technical question should not a get a vote up IMHO.
Chandan .
let alone be accepted!!
hasen j
How is this answer acceptable?! Absolute stupidity...
Neurofluxation
+9  A: 

I'm not an expert but I believe in the old days, arrays were stored in contiguous memory blocks, and the array index was also the offset to find a particular element. So if you add 0 bytes to the array's memory location, you get the first element. Adding 1 (byte, word, dword, etc.) gets the memory location for the 2nd element, and so on.

CodeMonkey1
The old days like yesterday and tomorrow. :)
Greg Hewgill
lol @ yesterday and tomorrow
Unkwntech
+15  A: 

This is nearly a duplicate of this question: http://stackoverflow.com/questions/393462/defend-zero-based-arrays

There are some good answers and links to other articles there. Specifically this one called "Why numbering should start at zero" by Dijkstra.

Mykroft
+2  A: 

The reason is that array access (like a[i]) can simply add i*sizeof(element) to the starting address. It's (slightly) faster than having to adjust it by 1 first.

Denis Hennessy
Which matters less and less as time goes on, while human readability matters more and more.
David Thornley
That's true, although using a 1-based array index would seem very unnatural now, at least for me.
Denis Hennessy
+3  A: 

Simple answer, the index value is "how from do I move from the start?"
(See pointer arithmetic)

The first item is at the start, you don't need to move...
The second item is one further onwards...
etc, etc

Dems
A: 

Because arrays in C are pointer arithmetic. myArray[0] indicates the start of the array, as does &myArray.

Also see this answer

notruthless
+60  A: 

Excerpt from Wikipedia:

The zero-based array is more natural in the root machine language and was popularized by the C programming language, where the abstraction of array is very weak, and an index n of a one-dimensional array is simply the offset of the element accessed from the address of the first (or "zeroth") element (scaled by the size of the element).

So basically it's because index math is easier given a zero lower bound.

mwigdahl
BTW congrats on your first >+10 answer.
Unkwntech
+1  A: 

I don't know the exact answer, but to me it makes sense because unsigned integers start at 0. So why waste a number? It also makes the underlying memory calculations easier.

mdm
+8  A: 

It is because an array is essentially a pointer to a memory location. Therefore, if the array actually contains a pointer to memory_location_1, the array + 0 bytes will point to the first element in the array.

Furthermore, if the array is a 32-bit integer array, array + 1 * 4-bytes ( array[1] ) will point to memory_location_1 + 4 bytes, thus landing you at the second element in the array.

My response is very breif and presumably difficult to understand if you're new to programming. The following tut is a bit more indepth, but it may require some general knowledge of C++: http://www.cplusplus.com/doc/tutorial/arrays.html

regex
A: 

It goes way back to the roots. If you think you hate that, take a look at this, because that's how we used to do it in the old days: http://en.wikipedia.org/wiki/Assembly_language#Assembler

It's also where the "0 based arrays" come from. It's not all that bad, really. It takes getting used to but it is by far not the most irritating of all language features.

And yes, you are right, it can easily be 1 based, the compiler can solve that for you because a computer language is just a "presentation layer" on top of some machine-code generating process.

Rolf
+28  A: 

How often do you really have to write myArray.length()-1? I find it very rare that I need to do that.

If you think that we all naturally start everything at 1, ask yourself:

  • What's the first minute of each hour?
  • How old are we during our first year of life?

There are counterexamples to this, of course (day of month, month of year, all kinds of stuff) - my point is it's not as obvious as natural as you seem to think. By contrast, in mathematical and computing terms it's absolutely natural to use 0 for all the reasons given in other answers.

Jon Skeet
I'll be the zeroeth one to comment: I agree, it depends on the situation :)
Daniel Daranas
My favourite counterpoint is the C library function mktime() where 0=January and 11=December.
Greg Hewgill
There's also : String[] myArray=new String[100]; and then remembering that the index of last element is 99. But i like your analogy regarding '0 years' and '0 minutes' :)
Click Upvote
A few days back I was doing date math in Excel: a zero time length formatted as a date is 1/1/00. it took me about 10minutes to notice why I got a different number counting on my fingers.
BCS
Sunday=0, Saturday=6. At least in the various programming languages I've seen...
Svish
@CU: 0-99 is a hundred elements. That makes perfect sense.
Adam Lassek
If you use 1-based arrays you will fix 50% of the annoyances and potential errors. Then you are dealing with the other 50% instead. Look up "off by 1 error".
gbarry
The Computer Science department at Warwick University refers to the ground floor as floor 0. Bless 'em.
Dominic Rodger
Sunday=0 is perverse - the week starts with monday :)
snemarch
+2  A: 

Maybe because 0 is the first number on a non-negative number line. When you count something you start at 0 you just don't say it out loud despite what you seem to be claiming the system starts at 0 you just don't use it.

Unkwntech
Except that if there's one rock there, I count "One" and stop, not "Zero" and stop. The first rock is rock number 1. The first element of std::vector<Rock> rock is element 0.
David Thornley
But if you were to index them you would still start with zero just like when you start counting you will start with zero you just don't say it out loud.
Unkwntech
+1  A: 

If this really bothers you, use Pascal. Arrays can start with whatever you want. The Wikipedia link I gave shows an example of an array starting with 1.

Mark Ransom
A: 

I agree with the answers of CodeMonkey1, Gamecat and mwigdahl

However I'd like to add that not all languages start at zero, Ada starts at 1. And note this behaviour is not just for arrays but also enums in Ada start at 1 but languages like C start their enums at 0 by default

hhafez
+2  A: 

Because if they started at 1, then the variable storing the array index would never be zero. This is wasteful.

Eli
+1  A: 

I would typically answer this with the "offset from an index in memory" response but I'm pretty sure that's not what you're looking for.

How about this speculation? Natural numbers is the set of non-negative numbers (especially in math/CS, some disciplines define them as the positive integers) -- {0, 1, 2, ...} -- so the math geniuses defining the "older" languages stuck with what they knew.

Austin Salonen
+1  A: 

The array index is the number of memory addresses that "cell" of the array is offset from the start of the array.

To get an element out of an array, all you need is the start location and the number of "spaces" to move - the index.

So, array[5] is five steps past the start of the array, or element 6, and array[0] is just the start of the array.

And yes, modern languages all really work like this. Start indexes other than one are just syntactic sugar. (Snazzy syntactic sugar, though.)

Electrons_Ahoy
+1  A: 

Because everything that I know would collapse if indices didnt start at zero.

I've been doing them that way for over 20 years, changing them now would set me back irreperably.

Neil N
A: 

Zero-indexing is more economical due to the way numbers are represented in binary digital computers. If you have a byte available for storing an index, you can store 256 indices using the range 0-255 but only 255 indices using 1-255.

As far as high-level programming languages go, I program about equally much in 0-based and 1-based languages and I can't say there's an advantage to either. I'm actually probably somewhat less prone to commit off-by-1 errors when working with 1-based indices, but that's by a small margin.

fredrikj
+7  A: 

Obligatory Quote:

Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration.
- Stan Kelly-Bootle

Gavin Miller
+2  A: 

It has nothing to do with performance. If arrays started at index 1 (which they do in some languages), the compiler could just shift the address of the array down by one element. In fact, you could do this yourself in C or C++ (it's not technically defined behavior, and not recommended though):

int zero_array[10];
int *one_array = zero_array - 1; // This is the address a 1-based language would use

assert(&zero_array[0] == &one_array[1]);

I prefer arrays that start with zero, because it's more convenient. Generally, ranges are easier to work with when they are half-open, i.e. start <= index < end.

Matthew Crumley
ditto on half open ranges
BCS
A: 
Tom A
@anon: What, no comment, just a down vote?! Tell me how C# is more usable than VB...
Tom A
I won't bother adding to the downvotes, but I also disagree with this answer. BASIC has had 1-based arrays for pretty much the same number of decades as C has had 0-based arrays. Hardly a reason for saying that it's "more modern".
Christian Hayter
-1. I can sacrifice 2 rep for such an incredibly ignorant answer.
Chazadanga