Thread: question about arrays

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Question question about arrays

    Why does the index of an array start at zero instead of one? Was it simply the preference of the creator of the language?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The array name is a pointer is disguise, it therefore has an address. The index is used to offset from that address... an offset of zero means you're at the beginning.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>The array name is a pointer is disguise, it therefore has an address.
    Close enough :-)

    >>Why does the index of an array start at zero instead of one?
    Think of it like this, each index is the number of items from the beginning using an offset
    Code:
    *(array + i)
    If indices started with 1 then how would you get a hold of the beginning? Since *(array + 1) is one item past the beginning you have to use an offset of 0 instead
    Code:
    *(array + 0)
    The reason C is like this is because it's faster and easier to write compilers using the offset instead of using something weird like starting with 1 because computers think in offsets, they start with 0 instead of 1 :-)
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM