the index is really an address offset from the first item in the array. when you set up an array, you are really setting up a pointer to a block of memory. the first item gets stored at location 0 in the block, so if you want to acces it you use the offset of 0. the next item will be at offset 1 and so on.
P.S. some languages use 1 for the first item.
2006-12-21 01:06:40
·
answer #1
·
answered by justme 7
·
0⤊
0⤋
While it is true that the array index starts with 0 in C it need not be the same everywhere. In pascal it starts with 1. The array element with index 0 is reserved for storing the length of the array.
It is also a disadvantage with C that the index starts with 0. If you view the index number as the number of elements before that element then in C v'll have to add 1 to the index. So the length of the array is one more than the index value of the last element.
2006-12-21 08:18:55
·
answer #2
·
answered by 1s 2s 2p 3s 3p 4s 3d 4p 5s 4d 4
·
0⤊
0⤋
I'm seeing this question come up over and over recently, so I hope you're not the same person asking it!
Anyway, the usage of 0 instead of 1 is a convention on higher level languages, but it makes perfect sense in lower level languages.
Consider what an array is. It's a giant list, which is really an arbitrary concept. The computer at the machine level is not aware of giant lists of items.
So what's really happening behind the scenes is that a large chunk of memory is being allocated. This huge block of memory represents the list. The list has some number of items, and guess what, this block of memory can be divided into that many smaller chunks.
Now you can refer to the block of memory by referring to the memory address (a pointer). But that pointer also ends up pointing to the first small chunk of memory.
Address -> [ _ _ _ _ _ _ _ _ _ ]
How do you refer to the next chunk of memory? It's address + sizeof(chunk of memory). The next chunk after that? address + 2*sizeof(chunk of memory).
So we see the pattern for referring to the memory locations is address + 0 * size, address + 1 * size, address + 2 * seize.
So now you can see why we use a 0 for the first item in an array. If you use a language like C or C++, quite commonly you pass by reference (use the pointers) so you need to refer to the first item with a 0. It makes sense then that the first item even in array notation [ ] is 0.
On higher level languages, the pointer concept might have been done away with, but it's now a convention to refer to the first item with 0, and not 1.
2006-12-21 07:59:49
·
answer #3
·
answered by csanon 6
·
1⤊
0⤋
In the lower level langauges it starts at zero because computers like zeros a lot more than ones. And as someone said that is also the offset you jump from the first element.
In the higher level lanagues it starts at zero because that's what everyone is used to.
Some languages may actually use one but not many.
2006-12-21 19:48:27
·
answer #4
·
answered by glenbrent 2
·
0⤊
0⤋
very tough question ...... sorry
2006-12-21 08:24:57
·
answer #5
·
answered by shuvadip d 3
·
0⤊
1⤋