4 ms·
With those other things zero is zero, but with arrays the zero index is the first item. The disconnect really starts when you get the size of an array and then
by chillfox 4y ago
With those other things zero is zero, but with arrays the zero index is the first item.
The disconnect really starts when you get the size of an array and then have to minus it by one to have the last index.
To take your ruler example and apply array logic to to. Say you are measuring a 2cm thing. If the ruler follows the index then it would show 1cm (the last index), and so you would have to add + to get the size/length.
Having the index not align with the size is a pain that results in a lot of +/-1 code that a compiler could just as easily have handled.
- fsckboy 4y agoit's a good idea to call the 0th element of an array the zeroth element, else you encourage off-by-one errors. The size of the array, the "count", is that which your index must be less than. I'm not saying it has to be this way and it can't start with 1, I'm saying what you are pointing out as a flaw is actually the mixing of systems. in computer programming teams it's best to adopt a standard unambiguous language to communicate to one another so as not have to constantly say "do you mean...?"
- iopq 4y agoNo, it's the first element, if it's a 32 bit int it still occupies bytes 0..4 the second int occupies bytes 4..8 you can see the array up to the second int occupies the contiguous space 0..8 which 0..4*2 and it has TWO elements