You are here:

C/Why array index starts at 0?

Advertisement


Question
Why array index starts with zero?

Answer
Hi Dear Snehal ,

My apologies for the delayed response :( .. well getting to the point

if we say a[0]; /* Means the first element of the array */
as you know well  'a' is the array name ( which holds the base address of the array ) say if the array starts in the memory location addressed 2000 then the value of 'a' ( Array name ) is 2000 ( you can ensure this by just printing the value of a ) the index value what you give inside the square bracket is also referred as offset (means the number of positions to be moved from the base address , this offsetting technique will vary based on the array type , say int , float , char ..)

if its an integer array pointer increment will be counted 2 ( since its two bytes of length , 4 for float and so on ).

so with the case of every integer pointer increment the address value will be incremented by two

for eg a[0] will be at the address 2000
a[1] will be at the address 2002 and so on
how come ?


The complier treats a[0] as *(a+0) ie *(a+0) (array name is
the base address of that array)gives the first element of
the array.
If the array starts from 1 a[1] is treated as *(a+1) gives
the second element of array.The first element is missing.so
array always starts from 0.

Hope this will help you out understanding little.
please get back to me if you require additional support

Thanks and Regards
Prince M. Premnath  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.