如何在这种情况下,数组索引工作性质再presentation?

编程入门 行业动态 更新时间:2024-10-11 21:27:32
本文介绍了如何在这种情况下,数组索引工作性质再presentation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在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?

本文发布于:2023-11-29 09:46:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   在这种情况下   索引   性质   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!