为什么从'0'数组索引开始 [英] Why Array index start from '0'

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

问题描述

我对C / C ++数组的一个问题。

I have a question on C/C++ arrays.

为什么数组的索引从开始'0'不是从'1'?

Why does the index of the arrays start from '0' not from '1'?

有任何数学的原因是什么?

Is there any mathematical reason for that?

推荐答案

在大多数编程语言,任何数组的名字是一个指针,它只不过是一个内存位置的引用,因此前pression阵列[n]的指向的存储器位置是从第一元件正元件程。这意味着该索引用作偏移。该阵列的第一个元素是恰好包含在阵列指向(0元件的距离),因此它应该始终称为阵列的存储器位置[0]

In most programming language, the name of any array is a pointer, which is nothing but a reference to a memory location, and so the expression array[n] points to a memory location which is n-elements away from the first 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 points to (0 elements away), so it should always be referred as array[0].

在进行[I]也可在[A + I]被表示为*(A + I)读为值,所以它总是起始于零。

a[i] can also be read as value at [a+i] which is denoted as *(a+i) , so it always starts at zero.

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

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