this post was submitted on 21 Nov 2023
420 points (93.9% liked)

Programmer Humor

31800 readers
600 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 9 months ago (2 children)

Haaaaaang on is that why we start on 0...

[–] [email protected] 5 points 9 months ago (2 children)

No. We count start at zero because the array already starts with an element of a specific size. Starting at 1 would always skip that initial element.

[–] [email protected] 8 points 9 months ago* (last edited 9 months ago)

You could have "empty arrays" in a language if you wanted. The real reason is that you start with an offset of zero as you read an array from memory at hardware level, and so this way address is just "start address + element size * element number".

[–] [email protected] 4 points 9 months ago* (last edited 9 months ago) (1 children)

No, we start counting at one. We start indexing at zero.

An array with one element has an element count of 1, and that element would be at index 0.

[–] [email protected] 3 points 9 months ago

This is how we end up with off-by-one errors

[–] [email protected] 0 points 9 months ago

Because if you convert it back to binary, you have 0x0000 and that is one extra bit you can use instead of limiting your available values.