为什么索引从0开始,而不是1 [英] Why index starts in C from 0 and not 1
问题描述
先生,
我知道数组索引从0开始于C而不是1可以是任何
身体。告诉我原因。
是因为在下标中我可以有一个无符号整数和
这些从0开始
谢谢
Sir,
I know that the array index starts in C from 0 and not 1 can any
body pls. tell me the reason.
Is it because in the subscript i can have a unsigned integer and
these start from 0
Thanks
推荐答案
" kapilk" < KA ********** @ rediffmail.com>在消息中写道
news:69 ************************** @ posting.google.c om ...
"kapilk" <ka**********@rediffmail.com> wrote in message
news:69**************************@posting.google.c om...
先生,
我知道数组索引从0开始,而不是1可以是任何
身体。告诉我原因。
是因为在下标中我可以有一个无符号整数和
这些从0开始
谢谢
Sir,
I know that the array index starts in C from 0 and not 1 can any
body pls. tell me the reason.
Is it because in the subscript i can have a unsigned integer and
these start from 0
Thanks
我认为这是由于编译器的工作方式。如果你有一个数组
sometype那么访问它们的方法使用符号
addressOfStartOfArray +(index * sizeof(sometype))
如果访问来自1,那么这将增加额外的计算,因此
因此会变慢。此外,几乎所有的编程语言都采用0作为初始索引。
Allan
I think it is due to the way that the compilers work. If you have an array
of sometype then the way to access these uses the notation
addressOfStartOfArray + (index * sizeof(sometype))
if the accesses were from 1, then this would add extra computation and
therefore be slower. Also, almost every programming language adopts 0 as
the initial index.
Allan
kapilk 2004年8月16日04:38:08 -0700写道:
kapilk on 16 Aug 2004 04:38:08 -0700 writes:
先生,
我知道数组索引从0开始,而不是1任何
身体。告诉我原因。
是因为在下标中我可以有一个无符号整数和
这些从0开始
Sir,
I know that the array index starts in C from 0 and not 1 can any
body pls. tell me the reason.
Is it because in the subscript i can have a unsigned integer and
these start from 0
恕我直言,这是一个随心所欲的决定,它只是有道理。
你可以把索引想象成一个值添加到基本指针。
#include< stdio.h>
int main(int argc,char * argv [])
{
/ *这里test是指向这5个字符中的第一个的指针。
五个字符在内存中是连续的* /
char test [5] = {''t'','e'','s'','t'',''\ n''};
printf("%c = =%c \ n",test [0],*(test + 0));
printf("%c ==%c\ n",test [1],* (测试+ 1)); / *加1你指向
到下一个字符* /
printf("%c ==%c\ n",test [2],*( test + 2));
printf("%c ==%c\ n,test [3],*(test + 3));
返回0;
}
-
Marco Parrone< ma *** @ autistici.org> [0x45070AD6]
IMHO no, it was an arbitrary decision, it just made sense that way.
You can think of the index like a value to add to the basic pointer.
#include <stdio.h>
int main (int argc, char *argv[])
{
/* here test is a pointer to the first of these 5 characters.
the five characters are consecutive in memory */
char test [5] = {''t'', ''e'', ''s'', ''t'', ''\n''};
printf ("%c == %c\n", test [0], * (test + 0));
printf ("%c == %c\n", test [1], * (test + 1)); /* adding 1 you point
to the next character */
printf ("%c == %c\n", test [2], * (test + 2));
printf ("%c == %c\n", test [3], * (test + 3));
return 0;
}
--
Marco Parrone <ma***@autistici.org> [0x45070AD6]
2004年8月16日星期一,kapilk写道:
On Mon, 16 Aug 2004, kapilk wrote:
先生,
我知道数组索引从0开始于C而不是1可以是任何
体。告诉我原因。
是因为在下标中我可以有一个无符号整数和
这些从0开始
Sir,
I know that the array index starts in C from 0 and not 1 can any
body pls. tell me the reason.
Is it because in the subscript i can have a unsigned integer and
these start from 0
我怀疑它与C语言是一种事实有关,这种语言旨在与硬件架构和大多数具有索引编址的汇编语言紧密配合。
语言模式从零开始。
另一方面,C语言起源于PDP-11。 PDP-11组件
语言只使用固定的源和目的地来完成诸如分配(MOV),加法(ADD),减法(SUB)和比较(CMP)之类的事情。 )。
换句话说,没有地址+偏移模式,如C68000或更多
现代处理器。
如果你相信这就是为什么C从零开始你将不得不问
问题,为什么汇编语言从零开始?但你必须在汇编语言新闻组中询问
。
-
发送电子邮件至:darrell at cs dot toronto dot edu
不要发电子邮件给 vi ************ @ whitehouse.gov
这篇关于为什么索引从0开始,而不是1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!