简体   繁体   English

在C中声明数组的大小

[英]Declaring size of Array in C

folks! 乡亲们!

I have an issue with the declaration of an array in C. I have to write a little program in Java which displays the amount of each letter in a text file as well as the total amount of letters. 我在C中声明数组存在问题。我必须用Java编写一个小程序,该程序显示文本文件中每个字母的数量以及字母的总量。 To educate myself further I also write every homework in C as well. 为了进一步教育自己,我也用C编写了每个作业。 In Java it completely works out, so I guess I have a big misunderstanding of arrays in C. 在Java中它完全可以解决问题,所以我想我对C语言中的数组有很大的误解。

I declared an int array with the size 26. So I thought the compiler allocates memory for 26 integers. 我声明了一个大小为26的int数组。所以我认为编译器为26个整数分配内存。 My output is a simple for loop with printf("%d",alphabet[m]); 我的输出是一个带有printf("%d",alphabet[m]);的简单for循环printf("%d",alphabet[m]); . But I get weird results. 但是我得到了奇怪的结果。 Some indexes are correct, some have very large numbers in it and others even have negative numbers. 有些索引是正确的,有些索引中的数字很大,而有些索引甚至是负数。 If I change the array size to int alphabet[2500]; 如果我将数组大小更改为int alphabet[2500]; it totally works out but I do not have any clue why... It does not make any sense, since with index 26 he should allocate 26 integers. 它完全可以解决,但我不知道为什么。。。这没有任何意义,因为使用索引26时,他应该分配26个整数。 Well apparently it make sense, otherwise it would not work. 显然,这很有意义,否则它将无法正常工作。 Has anyone a hint for me, I would appreciate it! 有谁对我的提示,我将不胜感激!

The text has a total amount of 5000 characters (whitespaces not included) with an average amount of ~150-200 per letter if this is important. 如果这很重要,则文本的总数为5000个字符(不包括空格),每个字母平均约150-200个字符。

My Code: 我的代码:

int len;
int alphabet[26];
int m;
FILE *handler;

if(handler!=NULL)
{
  for(m=0;m<=25;m++)
  {
    char buffer = 0;
    handler=fopen("text.txt","r");
    while((buffer=fgetc(handler)) != EOF)
    {
      if(buffer==(char)65+m)
      {
        alphabet[0+m]++;
      }

      if(isalpha(buffer))
      {
       len++;
      }
    }
    fclose(handler);
  }
}

[... else print(error) and fclose ...] [... else print(error)and fclose ...]

for(m=0;m<=25;m++)
{
  printf("[%c]  n: %5d \n",65+m,alphabet[m]);
}

You did declare the right sized array, but you didn't initialize it. 您确实声明了正确大小的数组,但没有初始化它。 That means it's potentially filled with random data. 这意味着它可能充满了随机数据。 Make sure to set it all to zero before using the ++ operator: 使用++运算符之前,请确保将其全部设置为零:

int alphabet[26] = { 0 };

There may be other errors, but one big problem with your code is that the elements of the array are not initialized before you attempt to modify them by applying the post-fix increment operator ++ . 可能还有其他错误,但是代码的一个大问题是,在尝试通过应用后缀增量运算符++修改数组的元素之前,未初始化数组的元素。 This is undefined behaviour , which may explain the "weird results". 这是未定义的行为 ,可以解释“奇怪的结果”。 You can zero-initialize all the elements of the array like this: 您可以像这样对数组的所有元素进行零初始化:

int alphabet[26] = {0};

Aside from the answers about initializing the array (this is vital, those are good answers), you'll want to rearrange your logic a bit I think. 除了有关初始化数组的答案(这很重要,这些都是好的答案)之外,我想您还需要重新排列一下逻辑。 Instead of looping over each element of alphabet, you can have each new character of input "drive" things. 不必遍历字母的每个元素,而是可以让每个新字符输入“驱动”事物。 For each character, check if it's a candidate for alphabet and adjust the count accordingly. 对于每个字符,检查它是否适合字母表,并相应地调整计数。 Right now, by looping over the alphabet in your for loop, you are doing everything 26 times which is not necessary . 现在,通过遍历for循环中的字母,您可以完成26次不必要的操作

So the only loop you need is the while loop. 因此,您需要的唯一循环是while循环。

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

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