Why we always start array index with a[0] not with a[1]? | SoloLearn: Learn to code for FREE!


Why we always start array index with a[0] not with a[1]?


11/13/2019 1:07:15 PM


14 Answers

New Answer


It can be best explained by a pointer. An array a[] can be thought as a pointer a(i.e. *a). So when we write a[0] compiler see it as *(a+0),a[1] as *(a+1) etc. Hence to get the first element of the array a we don't need to add any number to it. That's why indexing of the array starts from 0.


Depends on language to language There are quite a few languages where array indexing starts from 1 most notably are Fortran, Julia, Smalltalk etc. It's language design decision


In low level languages for example Assembler an Array with word items has a starting point in the stack were the first element is saved. Words have an offset of 4 in the memory adress. You can easily calculate the other item adresses of the array by multiplying the index with 4. So if the array would start with 1 the first word in the stack would be skipped.


Because of binary codes. They start with 00000000 (8 bits)(1 byte), second element of sequence is 00000001, next is 00000010 and so on....To translate for decimal base is necessary to retain the first element of sequence that determines the memory position.


Because it is designed like that [ and i think it should be. i didn't understand what your exact point. but think if it start from 1, in some cases for example, to search 0,10,20,..th index or 0,10,20.. values from array, you need to find manually means need search for all elements almost. you need more code. or else with a less code you may miss 0th involve values. if it from 0, you can do it simply %10 or /10, operations with less code , i may not accurate about your question, if it is not what u needed, ignore it please..] Almost every new modern languages follow zero based index approaches, it has advantages compare to 1baesd.


Index starts from 0 not 1 because it is created like that way. a = ['a', 'b', 'c'] | | | 0 1 2 print(a[0]) # output will be a The first element of the array is exactly contained in the memory location that array refers (0 elements away), so it should be denoted as array[0] . Most programming languages have been designed this way. There some old which uses Index from 1 too.


0 in the language is the location in the memory. 0 as 1 is the first element and the array points to the same location in memory, so it is basically saying that 1 (the first number) is 0 elements away from the location of the array itself. dont think of them as numbers, think of them as positions from base. 1 is the first number and 0 elements away from itself, 2 is the second number and is 1 unit away from 1 which is your base at 0. i hope that makes sense


Indexing from 0 more usefull if you do something like this case: If you have 25 indexing. And you want to spare using page, where every page for 10 item. Example php: $max = 10; $page = isset($_GET['page']) ? trim($_GET['page']) : 1; $max = 10; $start_indexing = ($page-1)*$max; If current page is 1, start indexing is 0. If current page is 2 start indexing is 10 etc. But if array start from 1, i thing the variable start indexing will add 1 on last line. It more long terms and long coding 😂😂 So begin from 0 it will be simple 😊


Because an array could be dimensionless and populated at run time.....


Most languages index from 0, which I admit can be very unintuitive. Another exception that I don’t think has been mentioned is Matlab, which indexes from 1.


👋 i would really love to follow u if u don’t mind



I am pakistani