A simple question to all programmers that why the index of an array starts from 0 instead from 1.
was it a mistake of compiler designer in history or it has some reason behind it.
Obviously it has a reason behind it. What is that reason?
Being honest to yourself, If you don't know the answer , Please join our site and contribute to society
was it a mistake of compiler designer in history or it has some reason behind it.
Obviously it has a reason behind it. What is that reason?
Being honest to yourself, If you don't know the answer , Please join our site and contribute to society
Because 0 is considered by most mathematicians to be a real number between -1 and 1 , and so in languages where arrays are positively indexed, zero is the first item (-1 is not possible, the first possible value then is 0).
I can’t say that it is very intuitive or that it matches reality (even though maths is supposed to map to reality): you can’t have zero of a thing, because by definition zero is none of everything.
In the context of comparison it makes sense and matches reality (i.e., you can have “space” for one apple in your basket, but not have an apple in it, and thus have 0 apples in your basket, or 1 space); but as a number like “0 apples, 1 apples, ...” it makes no sense, and thus it has special rules from all other numbers (e.g., it is the only number for which 0 + n = n, and 0 * n = 0 is always true).
But assuming that 0 is a real number, it does follow that positevly indexed arrays would start with it.
not a correct answer!!
it actually represents the offset of an array...
*(array_base + 0 )= *(array_base) , which is first element -->so index starting from 0 makes sense.