Why array indexes are zero based in most programming languages?

前端 未结 6 877
说谎
说谎 2020-11-29 13:46

C++, C#, C, D, Java,... are zero based.

Matlab is the only language I know that begin at 1.

6条回答
  •  孤街浪徒
    2020-11-29 13:53

    I guess because arrays use pointer arithmetic to refer to some value. Basically arrays have contiguous memory and if you want to refer to 5th element (a[4]) then a + 4 * size of int is performed

    Say if you start with 1 then to refer to 5th element you will have to do something like a + (5-1) * size of int

提交回复
热议问题