why-does-an-array-index-start-at-0-not-1?

Philippines
November 16, 2009 9:04am CST
would somebody give me a specific answer to my question??
3 responses
@WC1989 (595)
• United States
17 Nov 09
There are two reasons: 1. Storage. by starting at 0, it makes storage one bit smaller. 2. (the most probably explications) is because of the way arrays were first created. Originally arrays were really just pointers, which go to a specific location. And the index was designed to let the computer figure out what location was the next object located in. So it would do location + index * bits needed to jump. If the index started at 1, it would mean the reading could only start 1 space away from the original memory location due to the way the array/pointer arithmetic was created. So let's say you had a char array (I *think* char is 4 bytes but I could be wrong). And let's pretend you created an array at memory location 100. so char[] myarray; If you went to char[0] it would do 100 + 0 * 4 = 100. char [1] would do 100 + 1 * 4 = 104. This is practical because when such arrays are created, there is enough space saved so that the computer is able to store items at 100, 104,108, etc... I don't know if current programming languages still use that location format and index , but in slightly older languages (like C), where arrays are really pointers in disguise, it's crucial.
• Romania
14 Feb 10
Just a clarification : char is 1 byte in C and C++ on modern platforms . I recommend studying a bit of Assembly to really understand the way a computer works . You will understand the computer architecture .
@faisale83 (198)
• India
16 Jun 10
when defining array it allocate a memory on stack based on the array size. It' just point out the each memmory location by index numbers
• India
22 Jan 10
I have the same question. I am a php programmer. i think it is the concept who develop the array.