Array subscript

This is a discussion on Array subscript within the C Programming forums, part of the General Programming Boards category; why does the array index (for counting elements) start from 0(zero)? answer me please.... I'm a new user...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1

    Smile Array subscript

    why does the array index (for counting elements) start from 0(zero)?
    answer me please.... I'm a new user

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It makes for easier indexing into the array with pointer arithmetic. Many languages start their indexing at 0, and some start at 1 - it was the language designer's decision on what to start with.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by poovizhipanpa View Post
    why does the array index (for counting elements) start from 0(zero)?
    answer me please.... I'm a new user
    So that you can have an element numbered less than 1, silly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by poovizhipanpa View Post
    why does the array index (for counting elements) start from 0(zero)?
    answer me please.... I'm a new user
    The technical reason is that C is supposed to be an efficient language. If we have a chunk of memory (which is what our array is) that starts at address 1000, the first element of that is at address 1000, right?

    So, if we have indices starting at 1, the compiler would have to subtract one before it can find the actual memory address of the element we request, the calculation of a particular element is: baseaddress + ((index-1) * sizeof(element)). In the case of 0-based indices, there is no need to subtract anything, because baseaddress + (index * sizeof(element)) gives us the appropriate address.

    The 0-based index is thus a better solution for efficiency. It may not suit human beings quite as well as a 1-base index, but C was never intended to be "human efficient", but definitely the goal is to be "machine efficient".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    After using C++ for so long, it seems strange to me when I use languages that start their indexes at 1.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by cpjust View Post
    After using C++ for so long, it seems strange to me when I use languages that start their indexes at 1.
    I hear you - it just feels SO WRONG!!
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by poovizhipanpa View Post
    why does the array index (for counting elements) start from 0(zero)?
    answer me please.... I'm a new user
    Because if a[] is an array then a is a pointer to the first element; and a[n] is the same as *(a+n); therefore a[0] is the same as *(a+0) which is the same as *a which is the same as the value of the first element of the array, therefore a[0] must be the first element of the array. It all follows from the basic definitions.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. templates, unresolved external error
    By silk.odyssey in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2004, 04:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21