Live data from Hacker News

Why do arrays start at 0?

buttondown.email

261–270 of 702 posts

Re: Why do arrays start at 0?

#261

Earlier quoted context omitted.

Oddly(?) though, most parents of young children don't refer to a baby as being "zero years old." Rather, we break them down into smaller units: two days old, six weeks old, four months old.

It helps that change happens particularly fast at that age, so the difference between newborn and 6 months is worth mentioning. Later on the units go up again and we just say "I'm in my 30s" :P On the other hand a computer/car/house can also be 10 years old but not 0.

True, a few weeks can mean a few milestones at that age. It's still wild to me how quickly babies/kids develop. Every week brings new abilities, experiences, and emotions that weren't there before.

Re: Why do arrays start at 0?

#262
post #137

Earlier quoted context omitted.

A disadvantage comes to mind. While the following loop works as expected: for (size_t i = 0; i The following causes an unsigned integer underflow and is an infinite loop: for (size_t i = length - 1; i >= 0; i--) ...

the correct way/idiom to reverse iterate an array is for (size_t i = length; i-- > 0; )... It's surprising how often the issue pops, it works well with both signed and unsigned integers. (edit) I've started with one based indexing (basic)... mixed with 0 based (assembly), more 1 based (pascal), then more stuff (all zero based). I am, yet, to see a real advantage of a one based indexing... after the initial process.

Is this cache friendly?

Re: Why do arrays start at 0?

#263
For me, far more significant than anything else here is having to convert array indices into human readable text describing objects. And the objects are named by mechanical engineers who don't use 0 based indexing. So my code is full of

  print("Finger {}'s motor has overheated".format(i+1))

Re: Why do arrays start at 0?

#264
My favorite example of this confusion is JS and (original) Java dates:

    > new Date()
    Wed Aug 24 2022 ...
    > new Date().getFullYear()
    2022
    > new Date().getMonth()
    7
    > new Date().getDate()
    24
    
So 2022-08-24 comes out as (2022, 7, 24). One-based indexing for the year and day, zero-based indexing for the month.

Re: Why do arrays start at 0?

#265

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

But I think that still leaves open the question of "offset from what?" If you move zero from the first element, you're still at the first element... but if you move zero from the fourth element you're still at the fourth element. If you move one from before the first element, you're at the first element. I think you're more or less right, but I think we still need something to motivate the first element as the point…

In some languages/environments, the array is literally the same as the pointer to the first element.

Other languages are more sophisticated (like humans are!) and can say that position 0 does not exist. An array of size 0 has no elements. An array of size 5 has elements 1at through 5th. An array of size N has 1st through Nth, inclusive.

Re: Why do arrays start at 0?

#266
I started programming in assembly, then Fortran, Algol, COBOL, etc.

0 based index makes sense to me. The memory location is offset 0 bytes from where the first data byte is stored (without getting into endianism).

Re: Why do arrays start at 0?

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

A disadvantage comes to mind. While the following loop works as expected: for (size_t i = 0; i The following causes an unsigned integer underflow and is an infinite loop: for (size_t i = length - 1; i >= 0; i--) ...

You can always rip a page out of C++’s playbook:

    for (size_t i = length; i > 0; i--) {
        // ...
        item = array[i - 1];
(This is how reverse iterators work in C++.)

Re: Why do arrays start at 0?

#268

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…

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

The first floor in a USA building is not where a European would expect.

Re: Why do arrays start at 0?

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

That so people in this thread argue about the higher-level language (missing the point) shows that few people found access to the underlying machine code.

Which is sad, because all code is still executed as machine instructions even when the developer does not see it or does not want to care.

Re: Why do arrays start at 0?

#270
post #87

Earlier quoted context omitted.

Birthdays are anniversaries. Anniversaries are annual activities when we celebrate/honor past events. They do not include the event itself. The first anniversary is 1 year after the initial event.

> Birthdays are anniversaries. Indeed they are, which is why I literally said so in the previous comment. Did you forget to finish reading it? > They do not include the event itself. That is true because the event itself is implied information. There is no value in speaking of it. If you stand before us, we can be certain that you had a day of birth (your 0th anniversary). We don't necessarily know how many times you…

If I care about apples (for my lunch, or my store), I care about the difference between having 0 of them and not having bothered to count yet.

0!=null

Post reply on HN