WHY ARRAYS START AT 0 IN SIMPLE TERMS
WHY ARRAYS START AT (0) IN SIMPLE TERMS⠀
⠀
"When you make yourself into zero, your power becomes invincible!"
⠀
WTF MOMENT⠀
“Arrays start at 0”⠀
DISCLAIMER⠀
Not all languages have zero-based arrays. Array indices in Fortran, COBOL, MATLAB, R, Julia, and Lua start at 1.⠀
⠀
BOTTOM LINE⠀
Last thing first, the choice of zero-based or one-based (or any number) indexing is arbitrary. It comes from language design itself. There is no rule. Each option has its pros and cons. But, let’s see why zero-based indexing is more common!⠀
⠀
HERITAGE⠀
In C, an array points to its location in the memory, and the index is simply an offset that specifies the distance from the start. The first element is at the start of the array so there is no distance. Therefore the offset is 0. Using 0 makes pointer arithmetic easy and convenient.⠀
⠀
There you have it!⠀
⠀
So, even you can design a programming language in which array indices start from -1.⠀
⠀
However, using 0 is the most popular and common one because of the advantages it brings to the table!⠀
⠀
Share this post with your programmer friends!⠀
⠀
⠀
⠀
"When you make yourself into zero, your power becomes invincible!"
⠀
WTF MOMENT⠀
“Arrays start at 0”⠀
This is one of the first unusual things that beginners see in their programming journey. And most of us just memorize that fact and then laugh at memes about it. But seriously, why do array indices start at 0?⠀
⠀DISCLAIMER⠀
Not all languages have zero-based arrays. Array indices in Fortran, COBOL, MATLAB, R, Julia, and Lua start at 1.⠀
⠀
BOTTOM LINE⠀
Last thing first, the choice of zero-based or one-based (or any number) indexing is arbitrary. It comes from language design itself. There is no rule. Each option has its pros and cons. But, let’s see why zero-based indexing is more common!⠀
⠀
HERITAGE⠀
In C, an array points to its location in the memory, and the index is simply an offset that specifies the distance from the start. The first element is at the start of the array so there is no distance. Therefore the offset is 0. Using 0 makes pointer arithmetic easy and convenient.⠀
⠀
There you have it!⠀
⠀
So, even you can design a programming language in which array indices start from -1.⠀
⠀
However, using 0 is the most popular and common one because of the advantages it brings to the table!⠀
⠀
Share this post with your programmer friends!⠀
⠀
⠀
Comments
Post a Comment