为什么数组索引或基索引以0开头?

编程入门 行业动态 更新时间:2024-10-23 10:33:21
本文介绍了为什么数组索引或基索引以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开头?

本文发布于:2023-11-29 16:41:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   数组   开头

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!