3 ms·
0-based indexing is because of the original methods you would use to access sequential data. If you wanted to create an array with 100 1-byte integers you would
by ok_dad 4y ago
0-based indexing is because of the original methods you would use to access sequential data. If you wanted to create an array with 100 1-byte integers you would literally just take 100 bytes of memory and call it an array. To access the first item in that array, you would read 1 byte at memory location `array_start + offset` and for the 1st item you would have an offset of 0 bytes. For the 2nd item, it would be accessed at an offset of 1 bytes.
Here is an example of a 10-byte array with 1-byte items starting at address "5":
array cells xx xx xx xx xx xx xx xx xx xx
item number 1 2 3 4 5 6 7 8 9 10
mem offset 0 1 2 3 4 5 6 7 8 9
mem address 5 6 7 8 9 10 11 12 13 14
To get the 1st item, just take "5 + 0" to get the address to read from, and etc.
It's mostly because it was easier to just think in offsets starting from 0 rather than think in item numbers and have to always remember to use the "item number - 1" as the memory offset for that item.