Why array starts at 0? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 24

Why array starts at 0?

I was looking for good explanation of this question and best explanation I found is : much easier to create multidimensional arrays if indices start at 0, 'a[i*n+j]' vs 'a[(i-1)*n+j]' is this the reason or there is other reason as well.

28th Feb 2017, 2:26 AM
Ram chandra Giri
Ram chandra Giri - avatar
9 Answers
+ 10
Memory wise the index provided is an offset for the start of the array ( array pointer ). If you have an array of integers, you could access the integer at array[2] by taking the memory adress of array[0] and adding sizeof(int)*2 to it. This is how an array is beeing stored in your memory. So array[0] means starting adress of the array with 0 offset ;)
28th Feb 2017, 2:58 AM
siebenWinkel
siebenWinkel - avatar
+ 16
@Ace I do searched on google, stack overflow. The answer I got was easier to explain others but I asked for other reason that maybe in a book.
28th Feb 2017, 5:21 AM
Ram chandra Giri
Ram chandra Giri - avatar
+ 12
@Kuba Matlab maybe Fortan also. This was implemented after C era.
1st Mar 2017, 10:52 PM
Ram chandra Giri
Ram chandra Giri - avatar
+ 6
R starts them from 1 :)
1st Mar 2017, 8:50 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
it is a scheme called zero based indexing,in some languages they start with one
4th Mar 2017, 10:06 AM
Dave
Dave - avatar
+ 4
My first language was c.. Sometimes at night i still wake up from a nightmare where a dereferenced pointer, pointing at a pointer is doing a call by value..
28th Feb 2017, 3:09 AM
siebenWinkel
siebenWinkel - avatar
+ 4
@siebenWinkel I ever nightmare about .toString() !
28th Feb 2017, 3:10 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 2
I like siebenWinkels answer better, but I learnt it as: although in high level programming languages we write code in letters, digits and other characters, this gets turned into binary, and typically datatypes of programming languages have a set number of bytes that they get converted to so there is a limit on how many different values there are. Since it doesn't make logical sense to find the 0th position of an array, but the type an index is defined as contains a zero, you can maximise the potential size of arrays by starting at zero
2nd Mar 2017, 9:43 PM
Phil
Phil - avatar
0
Lol. U get upvote for ask like this. No body say try search on google. Not like when I asking a simple question.
28th Feb 2017, 4:21 AM
Ace
Ace - avatar