Live data from Hacker News

Why do arrays start at 0?

buttondown.email

31–40 of 702 posts

Re: Why do arrays start at 0?

#31

Earlier quoted context omitted.

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

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.

So how old is a not-yet-1-year old?

Re: Why do arrays start at 0?

#33

It 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…

Is it machine-focused that we can convert a distance of 1.5 m to 150 cm just by multiplying by 100?

Maybe distances should be human-focused, so that no displacement is equivalently expressed as 1 cm, 1 m, 1 km, ...

Then converting a distance d from m to cm is (d - 1) x 100 + 1.

Re: Why do arrays start at 0?

#34

Earlier 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.

So how old is a not-yet-1-year old?

N days or N months depending on how old the baby is.

Re: Why do arrays start at 0?

#35

It 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…

> 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) you stay at the first (index 1) item. Add one and ... you get the point.

Re: Why do arrays start at 0?

#36

Earlier quoted context omitted.

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

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.

Your birth day is the day you are born.

Your first birthday is the first anniversary of your birth day.

We celebrate the anniversaries of our birth day.

Re: Why do arrays start at 0?

#37

Earlier 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.

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.

Re: Why do arrays start at 0?

#38

Earlier quoted context omitted.

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.

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?

#39
I find 0-based indexing easier to work with when working with (dynamically sized) multi-dimensional arrays. For example, with 0-based indexing getting an element at (x, y, z) can be done with

  a[z * h * w + y * w + x]
But with 1-based indexing you need to substract one first from each component except the last:

  a[(z - 1) * h * w + (y - 1) * w + x]

Re: Why do arrays start at 0?

#40
Dijkstra's answer (linked in the article) is best:

"When dealing with a sequence of length N, the elements of which we wish to distinguish by subscript, the next vexing question is what subscript value to assign to its starting element. Adhering to convention a) yields, when starting with subscript 1, the subscript range 1 ≤ i https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...

Post reply on HN