如何在这种情况下,数组索引工作性质再presentation? [英] How does character representation in array index work in this case?
问题描述
我在C初学者,我有code是这样的:
I'm a beginner in C and I have code like this :
#include <stdio.h>
main()
{
int i;
int ndigit[10] = { [9] = 5 };
printf("%d\n",++ndigit['9']);
}
本打印的价值是这样的:
This prints the value something like this :
-1074223011
但是,当我改变语句:
But when I change the statement to:
++ndigit['9'-'0']
这是正确打印值
6
我不知道为什么有必要添加 -0
在我的指数,使其正常工作?又为什么只是提 ++ ndigit ['9']
,并不能帮助我?
I wonder why there is a need for adding -0
in my index to make it work properly? And why just mentioning ++ndigit['9']
, doesn't help me?
先谢谢了。
推荐答案
如果您要访问的第10个数组中的元素,你做的:
If you want to access the 10th element in an array, you do:
array[9]
如果你想拥有的字符常量数的值索引来访问元素 9
+ 1,你做的:
If you want to access the element at the index which has the value of the character constant for the number 9
+ 1, you do:
array['9']
由于方式ASCII(用C使用,其他所有字符编码方案,看绿野仙踪的评论)的定义,前pression '9' - '0'
实际上等于9,这可能会迷惑你在这种情况下。
Due to the way ASCII (and all other character encoding schemes used by C, see Wiz's comment) is defined, the expression '9' - '0'
actually equals 9, which might confuse you in this case.
这篇关于如何在这种情况下,数组索引工作性质再presentation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!