+ 5
Why we always start array index with a[0] not with a[1]?
Array
12 ответов
+ 7
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.
+ 3
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.
+ 3
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.
+ 3
Because an array could be dimensionless and populated at run time.....
+ 2
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 , need more complicated code. i may not accurate about your question, if it is not what u needed, just ignore it pls..]
Almost every new modern languages follow zero based index approaches, it has advantages compare to 1baesd.
+ 2
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.
+ 2
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
+ 1
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 😊
0
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.
- 4
👋 i would really love to follow u if u don’t mind