Why do arrays start at 0?
41–50 of 702 posts
Re: Why do arrays start at 0?
#42Re: Why do arrays start at 0?
#43It really comes down to a choice between a machine-focused (0) or human-focused (1) approach. The 0 makes a lot of sense in a C pointer world where memcpy and other alike functions can be written very thight. The 1 makes a lot of sense in a human world, when we count, we start at 1, we talk about the "1st", counting on finger starts with 1, etc. I once were at a Lua (1 indexed language) conference where this was disc…
> when we count, we start at 1, we talk about the "1st" Although often with an implicit zero. Under typical North American culture, your 1st birthday, for example, is more accurately the first anniversary of your birthday. Your birth is zero indexed.
Most human counting uses end-index. I.e. "1" is after 1-thing (has passed, is physically obtained, etc.).
Most computer counting uses start-index + length, for efficiency and to better generalize. I.e. "1" is at the memory address immediately before the "1"st item.
Which ultimately creates the "0 index is 1st thing" linguistic confusion.
PS: Also, language predates computers by a few years, and the concept of zero is hard.
Re: Why do arrays start at 0?
#44Re: Why do arrays start at 0?
#45It works better with the modulo operator: `array[i%length]` vs `array[(i+length-1)%length+1]`. Or you would have to define a modulo-like operator that maps ℕ to [1..n].
It works better if you have a multi-dimensional index, for example the pixels in an image. With 0 based indexing, pixel `(x,y)` is at `array[x+widthy]`. With 1 based indexing it is at `array[x+width(y-1)]`. You might argue that programming languages should support multi-dimensional arrays, but you still need operations like resizing, views, etc.
Re: Why do arrays start at 0?
#46Earlier quoted context omitted.
No, we don't count from 0. That's like saying we start counting a baker's cup from 0 because you can have half a cup.
If we (speaking as a North American) count from 1, does that mean other cultures (e.g. Korean) count from 2?
Re: Why do arrays start at 0?
#47It really comes down to a choice between a machine-focused (0) or human-focused (1) approach. The 0 makes a lot of sense in a C pointer world where memcpy and other alike functions can be written very thight. The 1 makes a lot of sense in a human world, when we count, we start at 1, we talk about the "1st", counting on finger starts with 1, etc. I once were at a Lua (1 indexed language) conference where this was disc…
If you think of the index as an offset you would start with 0. BTW on which level is the 1st floor?
Re: Why do arrays start at 0?
#48Arrays in programming use offsets.
for(int i=0;i
The error here is calling it 'i' for index. It should be 'o' for offset.Re: Why do arrays start at 0?
#49Earlier quoted context omitted.
No, birthdays are 1 indexed. Your Nth birthday is the day you turn N years old. When you're 1 year old, you've been alive for 1 year, not 2 years.
The moment you are born you are 0 years old (or perhaps 0.75 years old, but we don't usually recognize that). We count from zero in this case, at least implicitly. In some cultures you are considered 1 the moment you are born, so the zero indexing isn't universal here, but typical in North America as noted earlier.
Re: Why do arrays start at 0?
#50Earlier quoted context omitted.
If we (speaking as a North American) count from 1, does that mean other cultures (e.g. Korean) count from 2?
No, it just means we have a different notion of what a year is in regards to age. Similar to how different cultures can use different units of measurement for length, mass, etc.