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

查看:45
本文介绍了为什么数组索引从'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'?

这有什么数学原因吗?

推荐答案

在大多数编程语言中,任何数组的名称都是一个指针,它只不过是对内存位置的引用,因此表达式 array[n]指向距离第一个元素 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].

a[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天全站免登陆