28hua
V2EX  ›  问与答

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

  •  
  •   28hua · Oct 29, 2013 · 5685 views
    This topic created in 4760 days ago, the information mentioned may be changed or developed.
    C语言中一个拥有n个元素的数组其下标范围是0到n-1却不是1到n,这种设计有什么优势吗?
    10 replies  •  1970-01-01 08:00:00 +08:00
    fantasticfears
    fantasticfears
    Oct 29, 2013❤️1
    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
    krfantasy
    Oct 29, 2013
    2
    偏移量
    cctvsmg
    cctvsmg
    Oct 29, 2013
    3
    lua的下标为什么要从1开始? 反人类!
    AstroProfundis
    AstroProfundis
    Oct 29, 2013
    4
    angelface
    angelface
    Oct 29, 2013
    5
    数组的下标应该从0还是1开始?我提议的妥协方案是0.5,可惜他们未予认真考虑便一口回绝。
    ----Stan Kelly-Bootle
    angelface
    angelface
    Oct 29, 2013
    6
    10iii
    10iii
    Oct 29, 2013❤️1
    7
    a[i] == a + i;
    stanleesq
    stanleesq
    Oct 29, 2013
    8
    @angelface 笑尿
    28hua
    28hua
    OP
    Oct 29, 2013
    9
    @angelface 居然是手稿
    28hua
    28hua
    OP
    Oct 29, 2013
    10
    @fantasticfears 这样的话就不必老纠结多1少1的问题了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2574 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 11:18 · PVG 19:18 · LAX 03:18 · JFK 06:18
    ♥ Do have faith in what you're doing.