简体   繁体   English

奇怪的值被初始化为数组

[英]Strange values being initialized into array

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

When I initialize the array letters above, my assumption is that at the index of each capital letter, the value will be the number ie 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. 当我初始化上面的数组字母时,我的假设是在每个大写字母的索引处,该值将是数字,即如果['A'] = 4 ,则在索引'A'该值将为4,其余为未初始化的值默认为0。

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. 我不知道负数来自哪里。

'S' is the highest index you gave a value for and, as your encoding's apparently ASCII, the length of your array is 83 (= 0x53). 'S'是你给出的值的最高索引,因为你的编码显然是ASCII,你的数组的长度是83(= 0x53)。 Everything with smaller indexes (without an initializer) is initialized to 0 . 索引较小(没有初始化程序)的所有内容都初始化为0

When you print your array, you are accessing the array out-of-bounds, which is undefined behavior. 当您打印数组时,您正在访问数组越界,这是未定义的行为。

What your program probably outputs are the values which happen to be on the stack after your array. 您的程序可能输出的是阵列后恰好在堆栈上的值。 However, as said above, there is no guarantee about what will happen. 但是,如上所述,无法保证会发生什么。

HTH HTH

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;
}

And it works fine and prints everything up to and including 5 . 它工作正常,打印所有内容,包括5

You most probably are using a for loop that is structured as follows: 您最有可能使用的for循环结构如下:

for ( int i = 0; i < sizeof letters; i++ ) {
    // ...
}

And the problem lies within the sizeof letters part, which gives you the size of... letters , which is not how many elements the array has. 问题在于sizeof letters部分的大小,它给出了... letters的大小,这不是数组有多少元素。 It rather is, the size of letters , that is, size of a single element times amount of elements: 更确切地说, letters的大小,即单个元素的大小乘以元素的数量:

sizeof letters == sizeof * letters * amountofelements;
// evaluates to 1
// assuming that amountofelements is the amount of elements that
// the array letters points to

// more: sizeof * letters is equivalent to sizeof( letters[0] )
// and the equation above is equivalent to
// sizeof( letters ) == sizeof( letters[0] ) * amountofelements;
// just in case...

So, change your for condition into the following: 因此,将您for条件更改for以下内容:

for ( int i = 0; i < sizeof letters / sizeof * letters; i++ ) {
    // whatever
}

Thanks for giving me the opportunity to use my psychic powers, kek. 谢谢你让我有机会运用我的心灵力量,kek。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM