Live data from Hacker News

Why do arrays start at 0?

buttondown.email

151–160 of 702 posts

Re: Why do arrays start at 0?

#151

Earlier quoted context omitted.

Although, there is a fifth state in your example: When you are not in class. Which is different to an empty set that implies nothingness. When it comes to age, 0 being birth works well because there is truly is nothingness (from your perspective) before birth. When counting from 1 there is suggestion that there are variables that aren't worth speaking of because they are obvious.

Assuming there is nothingness for the fetus the entire nine months in the womb. For that matter, I can't recall being younger than four, so it's all nothingness before then.

Not really. Perspective isn't scientific and often cultural. Koreans, for example, famously have a different take and use a different counting systems to accommodate.

I am assuming that the reader is biased towards the average HN user. That won't always work for everyone who will come across my comment, but close enough for an unpaid contribution. I'm not about to write a novel to make sure I catch every edge case.

Re: Why do arrays start at 0?

#152

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.

When I'm baking I start by zeroing the scale, then adding enough flour to reach 500 g (or whatever the recipe calls for). That's counting from zero.

Re: Why do arrays start at 0?

#153

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

for (size_t i = 0; i EDIT: change i to j

Uh no don't reassign the loop variable in the inner scope. Use:

    const j = (length - 1) - i;
in that case. Much safer.

Re: Why do arrays start at 0?

#154
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--) ...

In that specific case I'd do the following:

    for (size_t i = n; i-- > 0 ;) ...
Or count from `length` to 1, but subtract 1 in the loop body, or count up and subtract the length in the loop body. Any modern compiler should be able to optimise these to be equivalent.

In the majority of cases, counting down is not necessarily. Nor is ordered iteration. Most languages have a `for each` style syntax that's preferable anyway.

Re: Why do arrays start at 0?

#156
post #56

Earlier quoted context omitted.

They do, it's called the ground floor.

In France, it is called the rez-de-chaussée. The “premier étage” (literally translated as “first floor”) is what the US calls the second floor.

A particularly topographically challenged building in Paris has exits to two enclosing streets end up on different floors. The elevator is numbered -2, -1, rez-de-rue [exit north], rez-de-chaussée [exit south], 1, 2, ... .

Re: Why do arrays start at 0?

#157
post #141

Earlier quoted context omitted.

Ah, yes. Cardinal numbers and ordinal numbers both end in the word "numbers" so they're the same thing. No need to distinguish between having three apples and having the third apple.

If you ask most people to just label their apples, they'd call the first one "1", and so on. Very few people would say, "please pass me apple #0".

And if someone did say that to me, I would pass them no apple, since that's how we use everyday language. And if I knew they were a programmer, I'd first ask them in which programming language they'd wish me to pass the apple.

Re: Why do arrays start at 0?

#158

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

for (size_t i = 0; i EDIT: change i to j

I hope you haven't done this anywhere.

Makes my brain hurt, but I think this will only run through the loop one time looking at the last element of the array.

Re: Why do arrays start at 0?

#159
Easy: I have an array with 256 elements. And I want to store the indexes of various elements of the array off in another data structure. So many of them, in fact, that it’s important that the index values each fit into a byte. So, I want the index values to be 0..255, and not 1..256, since 256 doesn’t fit into a byte.

Re: Why do arrays start at 0?

#160

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 That is false; counting begins by initializing an accumulator to zero. When you register the first item, the count jumps from 0 to 1.

Who sets an accumulator to zero and then adds one in everyday language?
Post reply on HN