Live data from Hacker News

Why do arrays start at 0?

buttondown.email

401–410 of 702 posts

Re: Why do arrays start at 0?

#401

There were a number of systems oriented languages that, while algol-like in syntax, used zero-based array indexing, predating C. BCPL was intended to be a generic systems oriented language, but Burroughs had ESPOL for their mainframe architecture, and HP wrote their operating systems and compilers in SPL, which was specific to their stack-oriented HP3000 architecture. All these used zero-based indexing, because for l…

On a cursory look, I'd say one based is more intuitive, for the sole fact that the a[len(a)] is the last element. Actually I remember there was a small time when I started programming in assembly it took me a while to get used to reasoning about the end of the array.

Re: Why do arrays start at 0?

#402
post #23

Arrays start at 0 because they're really just pointers. "[x]" is just shorthand for "+ x * sizeof". The first element is the one stored at the pointer address (0 sizeofs ahead of the pointer, the next element is stored 1 sizeof ahead of the pointer, etc.

In C, "a[x]" is effectively syntactic sugar for just "*(a+x)" (conversion rules for "+" means you don't need the sizeof). It also means you can reverse it and write e.g. "5[somearray]" if you really want to make developers want to throw rocks at you.

hehe, that was me :) when I was young.

Re: Why do arrays start at 0?

#403
post #361

Earlier quoted context omitted.

https://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/... Convenience. Look at how the days in the months are stored and accessed. Using 1-based months would introduce an extra calculation (-1) on all searches or an unused value in the 0-index. Also look at how printing is handled for weekday and month names. They, again, take advantage of 0-based indexing. Day and year are already represented as numbers, so it…

Subtracting 1 from the user provided index isn't that expensive. Or, if you're willing to give up three bytes, index into "ErrSunMonTueWedThuFriSat". Or, and this is mildly insane but perhaps in keeping with early C, have your pointer be three bytes before the beginning of "SunMonTueWedThuFriSat" and use one-based indexing.

It all boils down to the fact that C's memory model is meant to map closely to the physical memory model, and the C and Unix developers preferred simplicity of implementation over simplicity of interface. Leaky abstractions (like that the weekday or months start at 0 so that they can reference into arrays of names) are fine given that prioritization. Not everyone shares that preference.

Re: Why do arrays start at 0?

#404
post #219

Earlier quoted context omitted.

The ground floor is 0. The floor above 0 is 1. The floor below 0 is -1. Anything else is crazy TBH, the whole point of integer numbers is to count things that start at a defined point and go opposite ways. Why shift it and then reinvent weird pseudo-negative numbers like S1? But then again people measure distance in feet and write dates as month day year :)

Counterpoint: because floors are sum types. Floors 1 through 10 are "regular" floors. L is the lobby. P1 through P3 are the parking (basement) levels. So you have three types: regular, lobby, and parking. And the elevator labels are of type regular | lobby | parking. (Now you can't disagree with me because sum types are popular on HN, and I have framed this in terms of sum types.)

Sum types don't automatically define ordering between the possible values AFAIK? So in your implementation you'd have to manually define the

    up: (regular | lobby | parking) -> (regular | lobby | parking)

    down: (regular | lobby | parking) -> (regular | lobby | parking)
and

    which_way: (regular | lobby | parking) x (regular | lobby | parking) -> direction
If you use subset of integers you can just use operators inc, dec and Yeah I know it was a joke.

Re: Why do arrays start at 0?

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

Nats start at 0, end of discussion - it's only logical to index by the naturals.

That's really silly - the natural numbers also famously start at either 0 or 1, depending on the country, the discipline, and the individual.

Re: Why do arrays start at 0?

#406

Earlier quoted context omitted.

>If you count age, you start at birth (0) and count years; one for each birthday. nitpick: unless you were born on Feb 29.

I didn't know that. Do you get to be 1 year old if you are born on Feb 29, to account for leap years?

You're just 1/4 of the age of everyone else born the same year. You have the same number of laps around the sun as those people, but you've definitely had fewer birthdays. It's a common joke for leap year babies.

Re: Why do arrays start at 0?

#407
post #219

Earlier quoted context omitted.

The ground floor is 0. The floor above 0 is 1. The floor below 0 is -1. Anything else is crazy TBH, the whole point of integer numbers is to count things that start at a defined point and go opposite ways. Why shift it and then reinvent weird pseudo-negative numbers like S1? But then again people measure distance in feet and write dates as month day year :)

It would be nice if this was the case. In grad school I taught in a building that was on a hill and had once been separate buildings so different entrances were on different floor and the transition between the former separate buildings was 4 stairs. So there was a floor with rooms numbered 1xx and 0xx. There was also a floor below the 0xx floor. They just numbered it 00xx. And you could enter the building on any of…

You'd think if they had to go below 0 they could just prefix with a letter, like B (Below? Basement?) B1, B2, etc.

Re: Why do arrays start at 0?

#408
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.

But interestingly, the number at the top is 12, not 1.

Re: Why do arrays start at 0?

#409
post #376

Earlier quoted context omitted.

> It really comes down to a choice between a machine-focused (0) or human-focused (1) approach. Just think of 0-based as offset-based and 1-based as index-based. Both are intuitive just like that. I never get why people arguing over this bring pointers and memory (or anything computer related) to the table. No normal person is going to understand that, but everyone understands that if you don't move at all (0 offset)…

I must say in this case Google does it right: https://cloud.google.com/spanner/docs/reference/standard-sql... In Google standard SQL, to access an array it is simply not allowed to put a number inside square brackets. You must specify which way you mean. So `SELECT some_numbers[OFFSET(1)], some_numbers[ORDINAL(1)]` is allowed but not `SELECT some_numbers[1]`.

That sounds actually great to avoid any ambiguity. At the cost of a bit of verbosity, though.

Re: Why do arrays start at 0?

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

Amusing but probably irrelevant: the sailing yacht racing handicapping calculations mentioned are almost certainly under the Performance Handicap Racing Fleet (PHRF) system[0]. This system is "zero based" in its own way. A sailboat's performance is assessed and handicapped against a standard yacht with a zero rating. Your time to complete the race course is adjusted by your handicap to determine your final standing. This lets you race slower and faster types of boats in a "fair" way.

[0] https://en.wikipedia.org/wiki/Performance_Handicap_Racing_Fl....

Post reply on HN