Software Archive
Read-only legacy content
17061 Discussions

Why the base index of array is zero in c language?

Find_N_
Beginner
382 Views

I was trying to work with array in c language and wondered that the base index of array does not start with other than zero like it doesn’t start with 1, 2, 3.. I just want to know the concept behind.

“Why the base index of array is zero in c language” As I am a beginner in c programming language and I have more doubts about C issues, I will definitely post  and look for more c programming questions and answers in this community forum. Hope I will get genuine replies to my programming query.

0 Kudos
1 Reply
Akhil_R_
Beginner
382 Views

Array indices should start at 0. This is not just an efficiency hack for ancient computers, or a reflection of the underlying memory model, or some other kind of historical accident—forget all of that. Zero-based indexing actually simplifies array-related math for the programmer, and simpler math leads to fewer bugs. Here are some examples.
 

  • Suppose you’re writing a hash table that maps each integer key to one of 
    n
    buckets. If your array of buckets is indexed starting at 0, you can write 
    bucket = key mod n
    ; but if it’s indexed starting at 1, you have to write 
    bucket = (key mod n) + 1
    .
  • Suppose you’re writing code to serialize a rectangular array of pixels, with width 
    w
     and height 
    h
    , to a file (which we’ll think of as a one-dimensional array of length 
    w*h
    ). With 0-indexed arrays, pixel (
    x
    y
    ) goes into position 
    y*w + x
    ; with 1-indexed arrays, pixel (
    x
    y
    ) goes into position 
    y*w + x - w
    .
  • Suppose you want to put the letters 
    ‘A’
     through 
    ‘Z’
     into an array of length 26, and you have a function 
    ord
     that maps a character to its ASCII value. With 0-indexed arrays, the character 
    c
     is put at index 
    ord(c) - ord(‘A’)
    ; with 1-indexed arrays, it’s put at index 
    ord(c) - ord(‘A’) + 1
    .


It’s in fact one-based indexing that’s the historical accident—human languages needed numbers for “first”, “second”, etc. before we had invented zero. For a practical example of the kinds of problems this accident leads to, consider how the 1800s—well, no, actually, the period from January 1, 1801 through December 31, 1900—came to be known as the “19th century”.

0 Kudos
Reply