Live data from Hacker News

Why do arrays start at 0?

buttondown.email

551–560 of 702 posts

Re: Why do arrays start at 0?

#552
post #545
post #536

Earlier quoted context omitted.

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.

G or L (for lobby) followed by 1 isn't uncommon in the US.

> Confused me when I visited USA

It's applied inconsistently enough to be confusing for natives too!

Re: Why do arrays start at 0?

#553

Earlier quoted context omitted.

In Western music theory, intervals are one based. No pitch change is "unison"; one diatonic step is a "major second" and so on. As a result of this silly state of affairs, an octave occurs every 7 notes, even though the root "oct" means eight. Furthermore, a "rule of nines" is needed to invert an interval: e.g. inversion of minor 3rd is a major 6th (exchange major/minor, subtract from 9).

True, and it's a great example of how this whole drama is about a practical trade-off, not about a unique Right Answer. If you play piano, the second is the second finger; the fifth is the fifth finger, and it all makes sense. No problem. On the other hand trying to actually count that way (two thirds make a fifth and so forth) is maddening.

Clearly the solution is we need to start numbering fingers from 0.

Re: Why do arrays start at 0?

#554

Because otherwise you would be wasting a perfectly good number for no reason, which means you need to use more bits to do the same thing. To write 4 numbers (including zero) you only need two bits 0: 00 1: 01 2: 10 3: 11 To write 4 numbers if you avoid using the number zero, you need three bits 1: 001 2: 010 3: 011 4: 100 If you extrapolate that a little bit, you'll realize that you'll need two bytes (1 Byte + 1 bit…

Fron the article: > It's not the reason you think. No, it's not that reason either. Indexing is a language level concern, it has nothing to do with the generated code. Unless you write machine code I guess.

> Indexing is a language level concern

IIRC, Visual Basic 6 took a why not both? approach to array indexing.

Re: Why do arrays start at 0?

#555
post #45

Other advantages of zero based indexing, beyond being 'closer to the machine': It 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+width y]`. With 1 based index…

I think the 1-indexing folks would have to argue that a%b should return a value from 1 to b inclusive. This does make the same sort of intuitive sense as 1-indexing. For example we number clocks from 1 to 12.

No we don't. Clocks start at 0. what time is it when it's half an hour after midnight? 00:30.

Re: Why do arrays start at 0?

#556
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…

Waste 0th element or reuse it for something like length (hello, pascal strings). Another option is using base_address - element_size as your array value. Another option is using +element_size for all array accesses, assembly languages usually have this instruction. There’re many options to use 1-based indexing without sacrificing performance.

newer pascal strings are #0 terminated, original pascal strings were 255 chars max with string[0] being the string length, they were also not Unicode friendly, newer pascal compilers still support this as the shortstring type. I use pascal style strings in my embedded C library I wrote in 1989 much more efficient on 8/16-bit systems

Re: Why do arrays start at 0?

#557

Earlier quoted context omitted.

other important and day to day tools like rulers, clocks and speedometers also start at zero. So it's not exactly "bending humans to microprocessors weird alien ways". It all makes perfect sense in the context of measuring.

With those other things zero is zero, but with arrays the zero index is the first item. The disconnect really starts when you get the size of an array and then have to minus it by one to have the last index. To take your ruler example and apply array logic to to. Say you are measuring a 2cm thing. If the ruler follows the index then it would show 1cm (the last index), and so you would have to add + to get the size/le…

it's a good idea to call the 0th element of an array the zeroth element, else you encourage off-by-one errors. The size of the array, the "count", is that which your index must be less than.

I'm not saying it has to be this way and it can't start with 1, I'm saying what you are pointing out as a flaw is actually the mixing of systems.

in computer programming teams it's best to adopt a standard unambiguous language to communicate to one another so as not have to constantly say "do you mean...?"

Re: Why do arrays start at 0?

#558
post #545

Earlier quoted context omitted.

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.

G or L (for lobby) followed by 1 isn't uncommon in the US. > Confused me when I visited USA It's applied inconsistently enough to be confusing for natives too!

It's the consistent inconsistencies that causes it to be less confusing. It would be really weird to go from someplace very orderly to another that is haphazard.

Re: Why do arrays start at 0?

#559

Earlier quoted context omitted.

Another advantage is with ranges: 0-based indexing and exclusive ranges work well. This is apparent with cursor position in text selection Consider: Characters h e l l o Cursor index 0 1 2 3 4 5 Char index 0 1 2 3 4 Range [0,3) [0,1,2] Range [2,5) [2,3,4] Range [1,1) [] If we used 1-based indexing and exclusive ranges, it leads to ranges where the end index is greater than the string's length... Characters h e l l o…

This is the bane of my existence working with Lua. Iterating an array or adding to the end are fine, we have ipairs and insert for that, but ranges on strings I'm constantly having to think harder and write more code than necessary. I love the language, wouldn't trade it for another, but the 1-based indexing on strings, which represents an empty string at position 3 as (3,2), it's egregious. Not as egregious as a dyn…

> a dynamic language where 0 is false though.

well, C also considers 0 being false (and you can argue that C is "dynamic"!).

Post reply on HN