Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: why the array index has to start at 0??

by Marshall (Abbot)
on Jun 23, 2009 at 10:46 UTC ( #773960=note: print w/replies, xml ) Need Help??

in reply to why the array index has to start at 0??

This arises from how the basic "machine" (the hardware) works.

Let's say that you have an array of 3 "things" starting at address "4". I am going to make a simplifying assumption here that each "thing" fits within the native "word size" of the hardware machine. So what is the machine address of the first thing in the array? It is address 4+0 (it is at address 4). What it the address of the 2nd thing? That is address 5=4+1, etc..

Some languages will do an index-1 for you automatically, I seem to remember that FORTRAN does this. Maybe Pascal did this also? It has been a very long time since I've used these languages and my memory is hazy.

Anyway some languages do a "-1" for you. This slows things down a very slight bit, but does it matter? Probably not in the case of a single array.

This address 4+0 idea is very powerful when working with loop constructs and has the ability to simplify the code when working with "pointers" or "references".

  • Comment on Re: why the array index has to start at 0??

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://773960]
[karlgoethebier]: silence after a post is the worst punishment ;-)
[karlgoethebier]: i guess i've been ikegamid, he

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2017-11-23 17:04 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (336 votes). Check out past polls.