Live data from Hacker News

Why do arrays start at 0?

buttondown.email

541–550 of 702 posts

Re: Why do arrays start at 0?

#541

Earlier quoted context omitted.

Except 1-based indexing is what we use in normal language. We don't use "zeroeth" or "player (number) zero" etc. And the word "first" is shortened to 1st etc. Personally I think we'd be better off if programming languages stuck to the same convention - off-by-1 errors aren't the hardest problems to deal with but they're still annoying.

Well: https://www.merriam-webster.com/dictionary/zeroth

Sure, it's technically a word, but hardly one you'd casually drop into your conversations (with non-programmers)

Re: Why do arrays start at 0?

#542
In defense of starting at 1: if you're counting things, starting at 1 ensures that the last number you said is equal to the number of things you've counted so far.

Toddlers love this invariant.

Re: Why do arrays start at 0?

#543

Earlier quoted context omitted.

> additional computational overhead at compile time, not runtime.

For constant addressing; for arr[i] = 2, you'll still need to subtract 1 from i with 1-based addressing when converting to machine instructions.

For most cases you could get away with some index transformation logic in the compiler (but probably your language would need special types for indices, so I do agree with you that it is not worth the trouble).

Re: Why do arrays start at 0?

#544

Earlier quoted context omitted.

I would be fine with it all if 0-based was called "offset", with "index" reserved for 1-based. The element at offset 0 is the first element. Precision in naming is important.

Defining index as the element wise offset is a precise well formed definition/name. It's also a definition commonly used in math, through not always. It depends a lot of the area of mathematics and even the cultural context. The only reason we sometimes feel 0-index is wrong IMHO is because the english language describes entities in a sequence as 1st, 2nd, 3rd etc. But I wouldn't be surprised if there is some human l…

> Defining index as the element wise offset is a precise well formed definition/name.

But it's less precise than offset and obviously introduces confusion. Whoever originated the term "0-indexed" should have just suggested we use a better word rather than keeping the inaccurate word (proven by the fact you're currently looking for a way to remove some confusion around the currently chosen word) and prefixing it with a digit, which itself is a confusing thing to do to an English word, thus adding to the confusing while subtracting none.

Re: Why do arrays start at 0?

#545
post #536

Earlier quoted context omitted.

Defining index as the element wise offset is a precise well formed definition/name. It's also a definition commonly used in math, through not always. It depends a lot of the area of mathematics and even the cultural context. The only reason we sometimes feel 0-index is wrong IMHO is because the english language describes entities in a sequence as 1st, 2nd, 3rd etc. But I wouldn't be surprised if there is some human l…

Which floor is the first floor in your building? Depends which country your building is in, and more! My apartment building is 1,2… but my mall is G,M,1,2… (same country different architects). For many years I lived in Europe where it’s usually G,1,2… (where G can also be E or Fsz or whatever) and when I was in the US I had to remember that 1 is G, though L,2,3… is also common. City people live with index ambiguity a…

Yes, where I'm at — large country in Asia — we count floors starting from 0 (aka G for Ground).

Confused me when I visited USA.

Re: Why do arrays start at 0?

#546
After programming in C for a while, I really began to have the feeling that arrays were physical things that took up space in RAM. When I think like this, starting an index at 0 is very intuitive, as 0 implies the origin point of the physical construct.

If you think of arrays just as a kind of abstract list, then beginning at 1 makes much more sense. No one who looks at a to-do list at home talks about the 0th thing on their list to do. But the spacial nature of arrays makes starting at 1 confusing. After I take 1 step, I am no longer at my starting point. And cycling through arrays in code for me has a very similar feeling to taking actual physical steps through a data structure.

Re: Why do arrays start at 0?

#547

Earlier quoted context omitted.

I think, in theory, it would work regardless of the starting address. As long as you don't try to access the invalid address (which you wouldn't assuming that it's starting in the index 1, you would always be accessing the first valid address)

In theory, it’s not guaranteed to work at all. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf#p... (emphasis added): “In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i + n-th and i − n-th elements of the array object, _provided_they_exist” […] If both the pointer o…

That is a description of the commonly agreed upon definition of the C abstract machine and language semantics. You could simply define the language another way with regards to this behaviour.

Not that I would want to do it, I think zero-based addressing is not very taxing for the convenience of being closer to how we think of memory addressing.

Re: Why do arrays start at 0?

#548
post #235

I don't quite understand the argument "0-based being easier for pointer arithmetic is nonsense because the language doesn't have pointers". Whether or not the language presents the concept of "pointer" to the user is independent of whether or not it uses pointers internally. And if it exposes arrays as a concept, it has to implement them somehow. The simplest possible implementation of arrays is having a start addres…

I would be fine with it all if 0-based was called "offset", with "index" reserved for 1-based. The element at offset 0 is the first element. Precision in naming is important.

But we'd have to change all the "i"s in our for loops with "o"s across the planet, maybe even beyond. I'm willing to help, whatever it takes.
Post reply on HN