Live data from Hacker News

Why do arrays start at 0?

buttondown.email

701–702 of 702 posts

Re: Why do arrays start at 0?

#701
post #688

Earlier quoted context omitted.

Allowed in Rust: https://play.rust-lang.org/?version=stable&mode=debug&editio... it's an array of length 0, but it can't be "used" because it has a length of 0 so it occupies no RAM

> An array of length 0 starts at 0 and ends with 0 But such an array does not "start" at 0 as it does not exist. You need an array of at least length-1 to "start" at 0.

I can write a loop like this:

`for(int i=0; i < 0; ++i)` and it will run 0 times, the exact amount of times I expected

Re: Why do arrays start at 0?

#702
post #701

Earlier quoted context omitted.

> An array of length 0 starts at 0 and ends with 0 But such an array does not "start" at 0 as it does not exist. You need an array of at least length-1 to "start" at 0.

I can write a loop like this: `for(int i=0; i < 0; ++i)` and it will run 0 times, the exact amount of times I expected

But again your not starting at "index 0" of an array that exists.

This loop is not processing any physically existing array. In fact the compiler would remove this block of code as it is a no-op.

Post reply on HN