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…
Why do arrays start at 0?
141–150 of 702 posts
Re: Why do arrays start at 0?
#142If your language doesn't support zero-indexing, then you are stuck with 1-indexing, and that's that.
So, zero-indexing is the natural thing to put in a language, as it accommodates everybody.
Re: Why do arrays start at 0?
#143It 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…
Doing the reversal test, if programming languages had all been 1-indexed, then I doubt we'd hear much from people, in 2022, saying "I think the first element should be 0, and the second 1".
Re: Why do arrays start at 0?
#144Earlier quoted context omitted.
No. 1. Optimizing compilers exist. 2. Addressing at fixed offsets is cheap (a single instruction): https://en.wikipedia.org/wiki/Addressing_mode 0-based indexing is superior (as a default) because it simplifies a lot of common math, as discussed in TFA.
TFA = Thanks For Asking?
Re: Why do arrays start at 0?
#145Earlier quoted context omitted.
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.
What do you do for a living, and why are they paying you not to understand this?
Re: Why do arrays start at 0?
#146Earlier 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.
Re: Why do arrays start at 0?
#147Earlier 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
Re: Why do arrays start at 0?
#148Earlier quoted context omitted.
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.
The counting activity doesn't begin when the first item is registered; it begins when the counter is initialized to zero. A decision is made to begin counting, along with the realization that nothing has been counting yet. That's when counting has started. When the first item is seen, the counting is then continuing. Suppose your job is to count some events. You check in for work at 8:00 a.m., but the first event has…
Re: Why do arrays start at 0?
#149It 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…
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.
Re: Why do arrays start at 0?
#150Earlier quoted context omitted.
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.
The counting activity doesn't begin when the first item is registered; it begins when the counter is initialized to zero. A decision is made to begin counting, along with the realization that nothing has been counting yet. That's when counting has started. When the first item is seen, the counting is then continuing. Suppose your job is to count some events. You check in for work at 8:00 a.m., but the first event has…