search
HomeBackend DevelopmentPython Tutorial为什么大多数编程语言中的数组都是从0开始计数的,比从1开始计数有什么好处?

回复内容:

提供两个链接:
1. Why does the indexing start with zero in 'C'? (stackoverflow.com/quest)
2. Why numbering should start at zero, by Dijkstra. (cs.utexas.edu/users/EWD) 不是全部,Pascal类语言就可以不从0开始计数,比如从-100..100 考虑C语言指针
int a[10];
那么 a[0] === *(a+0)
我认为是这么考虑的 先说说为什么C语言的数组是从0开始
众所周知,C语言的数组是直接操作内存,那我们肯定得从内存的寻址开始说起:
以“以行为主序”的分配为例:设数组的基址为LOC(a c1 c2),每个数组元素占据l 个地址单元,那么aij 的物理地址可用一线性寻址函数计算:
LOC(aij)=LOC(a c1 c2)+( (i- c1) *( d2 - c2 + 1)+ (j- c2) )*l
推广到一般的三维数组:A[c1..d1] [c2..d2] [c3..d3],则aijk 的物理地址为:
LOC(i,j,k)=LOC(a c1 c2 c3)+( (i- c1) *( d2 - c2 + 1)* (d3- c3 + 1)+ (j- c2) *( d3- c3 + 1)+(k- c3))*l
显然,此处的c1 c2 c3 为0 会大大简化计算有木有啊!!越是多维数组效果越明显
这对于计算机寻址计算来说显然好处是大大的啊
这就是为什么最早C语言的数组起始都是0
至于什么其他的原因觉得美啊什么的都是后来人们YY的
事实上,在现在很多时候计算不再是瓶颈的时候,数组的下标也有从1开始的
比如matlab
难道matlab就不美了么= = 从技术实现上讲,数组的下标就是内存的偏移量吧。如p指针指向数组的内存,p+0就是第一个元素的内存地址,p+n ×sizeOfElement就是第n个元素的内存地址 学C语言的时候这么理解过:
数组名a就是数组第一个元素的内存地址
而取数组元素,就是通过a地址访问内存的过程
取第1个元素,就是*(a+0)
取第2个元素,就是*(a+1),
...
所以下标从0开始 不见得吧,ObjectPascal中数组可以从1开始,这个“从零开始”的问题,我个人认为这是个人为的历史遗留问题,就像现在的计算机键盘的字母分布,没有什么特殊理论依据 很多语言数组不是从0开始的(比如 MATLAB),但是一位计算机科学先驱 Edsger Dijkstra(发明 Dijkstra 最短路算法的那位)在 1982年写过文章,推崇从0开始做下标,叫 “为啥得从0开始数数”。

全文在这里:Why numbering should start at zero (EWD 831)

总结一下:
  1. 描述自然数子序列,上界和下届的差应该是子序列的长度,数组下标可以理解为一个特殊的这种子序列。
  2. 下界应该包含数据内的元素,上届应该不包含。换句话来说,下界应该是数组第一个索引。否则可能对于某些子序列,我们得用非自然数的实数作为下界。
  3. 如果考虑条件(1) 和 条件(2),描述上界和下界有两个办法:1
翻译自 Quora Travis Addair's answer to Why do array indices start with 0 (zero) in many programming languages?
不过翻译完了感觉有些不大对劲。描述区间(根据最大值和最小值判定的实数集合)和描述程序语言中数组元素是两码事吧。。。还是从C语言的实现上解释比较靠谱。 在硬件上 计数什么的都是从所有位全部为低电平(正逻辑)开始的,也就是无符号整型里边的0。 0 0是+运算的幺元,1是*运算的幺元.
*比+用的多,所以用0.
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6?What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6?Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to improve the accuracy of jieba word segmentation in scenic spot comment analysis?How to improve the accuracy of jieba word segmentation in scenic spot comment analysis?Apr 02, 2025 am 07:09 AM

How to solve the problem of Jieba word segmentation in scenic spot comment analysis? When we are conducting scenic spot comments and analysis, we often use the jieba word segmentation tool to process the text...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.