V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
28hua
V2EX  ›  问与答

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

  •  
  •   28hua · 2013-10-29 12:20:38 +08:00 · 4332 次点击
    这是一个创建于 3841 天前的主题,其中的信息可能已经有所发展或是发生改变。
    C语言中一个拥有n个元素的数组其下标范围是0到n-1却不是1到n,这种设计有什么优势吗?
    10 条回复    1970-01-01 08:00:00 +08:00
    fantasticfears
        1
    fantasticfears  
       2013-10-29 12:30:37 +08:00   ❤️ 1
    对于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
        2
    krfantasy  
       2013-10-29 12:34:52 +08:00
    偏移量
    cctvsmg
        3
    cctvsmg  
       2013-10-29 12:36:20 +08:00
    lua的下标为什么要从1开始? 反人类!
    AstroProfundis
        4
    AstroProfundis  
       2013-10-29 12:40:35 +08:00
    angelface
        5
    angelface  
       2013-10-29 12:46:51 +08:00
    数组的下标应该从0还是1开始?我提议的妥协方案是0.5,可惜他们未予认真考虑便一口回绝。
    ----Stan Kelly-Bootle
    angelface
        6
    angelface  
       2013-10-29 13:06:58 +08:00
    10iii
        7
    10iii  
       2013-10-29 13:56:43 +08:00   ❤️ 1
    a[i] == a + i;
    stanleesq
        8
    stanleesq  
       2013-10-29 14:53:43 +08:00
    @angelface 笑尿
    28hua
        9
    28hua  
    OP
       2013-10-29 23:05:42 +08:00
    @angelface 居然是手稿
    28hua
        10
    28hua  
    OP
       2013-10-29 23:06:55 +08:00
    @fantasticfears 这样的话就不必老纠结多1少1的问题了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1084 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:19 · PVG 07:19 · LAX 16:19 · JFK 19:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.