为什么数组索引或基索引以0开头? [英] Why does an array index or base index start with 0?

查看:60
本文介绍了为什么数组索引或基索引以0开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用任何语言的数组时,我总是想知道为什么数组的基地址或索引号从零开始.

While working with arrays in any language, I have always wondered why does the base address or index number of an array start with zero.

int x[5]={21,34,55,314,45};

现在,如果我要访问数组的第一个值,则必须使用 x [0] ,但是为什么0后面有逻辑呢?

now if I want to access any the first value of an array I would have to use x[0], but why 0 is there any logic behind this?

推荐答案

在C中,数组的名称本质上是一个指针,是对内存位置的引用,因此表达式array [n]表示内存位置.n个元素远离起始元素.这意味着将索引用作偏移量.数组的第一个元素正好包含在数组所引用的存储位置中(相距0个元素),因此应将其表示为array [0].大多数编程语言都是以这种方式设计的,因此从0开始的索引是该语言固有的,因为大多数语言(不是全部)都遵循C标准.您可以引用此链接了解更多信息.

In C, the name of an array is essentially a pointer, a reference to a memory location, and so the expression array[n] refers to a memory location n-elements away from the starting element. This means that the index is used as an offset. The first element of the array is exactly contained in the memory location that array refers (0 elements away), so it should be denoted as array[0]. Most programming languages have been designed this way, so indexing from 0 is pretty much inherent to the language as most of the languages (not all) follow C standards. You can refer this link for more details.

这篇关于为什么数组索引或基索引以0开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆