This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where array index starts from 0

 
Deepika Wadhwa
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
i have a basic confusion..
why does array index start from zero ?
is there any specific reasond this ?

thanx in advance
Deepika
 
sandeep jakkaraju
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know the answer to this ...
but i can say that ..the concept of first element of an array being 0 (ZERO) is INDIAN !! for the obvious reasons ;-)

Originally posted by Deepika Wadhwa:
Hi all,
i have a basic confusion..
why does array index start from zero ?
is there any specific reasond this ?

thanx in advance
Deepika

 
Colin Kenworthy
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no reason for it other than maybe it was copied from another language like C/C++ ? In COBOL arrays start at 1.
It sometimes catches me out ;-D
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may be right when you say it is copied from C/C++ (which may have copied ASM 68000). In these languages, tab[i] refers to a memory adress situated at tab+i*sizeof(tab elements). So it is easier to refer to 0 as being the first element, otherwise it would have needed one more arithmetic operation: tab+(i-1)*sizeof(tab elements).
Any other ideas?
W.
 
matt hooker
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, Wilfred is spot on. The reasons for array initialisation at 0 are very low level memory address type stuff, and indeed, low level logic dictates starting at 0, so there is an (uneccessary??) overhead in manipulating this default value for the purposes of our code.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic