奇怪的值被初始化到数组 [英] Strange values being initialized into array

查看:143
本文介绍了奇怪的值被初始化到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int letters[] = {['A'] = 4, ['B'] = 8, ['E'] = 3, ['I'] = 1, ['O'] = 0, ['S'] = 5};

当我初始化上述阵列信,我的假设是,每个大写字母的指数,该值将数即如果 ['A'] = 4 ,然后在指数'A'的值将是4和未初始化值的其余部分将被默认为0。

When I initialize the array letters above, my assumption is that at the index of each capital letter, the value will be the number i.e. if ['A'] = 4, then at index 'A' the value will be 4 and the rest of the values not initialized will be 0 by default.

但是,当我打印的字母排列的所有值,我得到这样的输出:

But when I print all the values of the array letters, I am getting this output:

00000000000000000000000000000000000000000000000000000000000000000480030001000000000514303876720941309621-1392458268143038767232767-197939865932767-1979398659327670010143038792832767

00000000000000000000000000000000000000000000000000000000000000000480030001000000000514303876720941309621-1392458268143038767232767-197939865932767-1979398659327670010143038792832767

我不知道那里的负数的来源。

I have no idea where the negative numbers are coming from.

推荐答案

我的猜测是,code你写的打印阵列是错误的。就在这个测试:

My guess is that the code you wrote to print the array is wrong. Just tested with this:

#include <stdio.h>

int main()
{
  int letters[] = {['A'] = 4, ['B'] = 8, ['E'] = 3, ['I'] = 1, ['O'] = 0, ['S'] = 5};
  int i;

  for (i = 0; i <= 'S'; i++) {
    printf("%d\n", letters[i]);
  }

  return 0;
}

和它工作正常,一切都打印直至及包括 5

And it works fine and prints everything up to and including 5.

这篇关于奇怪的值被初始化到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆