C语言的数组下标为什么要设计成从零开始

2013-10-29 12:20:38 +08:00
 28hua
C语言中一个拥有n个元素的数组其下标范围是0到n-1却不是1到n,这种设计有什么优势吗?
4369 次点击
所在节点    问与答
10 条回复
fantasticfears
2013-10-29 12:30:37 +08:00
对于i = 0; i < N和i = 1; i <= N的循环次数计数来说N - 0 = N次和N - 1 + 1 = N,前者是更直观的,详细见Dijkstra对此的解释:http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF

[1] stackoverflow, Why does the indexing start with zero in 'C'? http://stackoverflow.com/questions/7320686/why-does-the-indexing-start-with-zero-in-c
[2] Guido van Rossum about why python uses 0-based index https://plus.google.com/115212051037621986145/posts/YTUxbXYZyfi
krfantasy
2013-10-29 12:34:52 +08:00
偏移量
cctvsmg
2013-10-29 12:36:20 +08:00
lua的下标为什么要从1开始? 反人类!
AstroProfundis
2013-10-29 12:40:35 +08:00
angelface
2013-10-29 12:46:51 +08:00
数组的下标应该从0还是1开始?我提议的妥协方案是0.5,可惜他们未予认真考虑便一口回绝。
----Stan Kelly-Bootle
angelface
2013-10-29 13:06:58 +08:00
10iii
2013-10-29 13:56:43 +08:00
a[i] == a + i;
stanleesq
2013-10-29 14:53:43 +08:00
@angelface 笑尿
28hua
2013-10-29 23:05:42 +08:00
@angelface 居然是手稿
28hua
2013-10-29 23:06:55 +08:00
@fantasticfears 这样的话就不必老纠结多1少1的问题了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/87267

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX