运行 尝试创建结构数组时时间检查失败#2

Run Time Check Failure #2 when trying to make an Array of Struct

我收到此代码的运行时错误:

#include<stdio.h>
#include<string.h>

#define SIZE 27

struct CountABC
{
    char c;
    int n;
}typedef CountABC_t;

char commonestLetter(char* filename)
{
    CountABC_t ABC[SIZE];
    int i,count=0;
    char cha;
    for (cha='A', i=0 ; i <=SIZE ; cha++,i++)
    {
        ABC[i].c= cha;
        ABC[i].n = 0;
    }
    //printf("%c",ABC[3].c);

}


void main()
{
    char* str = "ABBA GADDA LOLLAL";
    char str2[SIZE];
    char ch;
    FILE* f = fopen("input.txt","w");
    if (f == NULL)
    {
        printf("ERROR! Cannot Find Any File!!");
        return;
    }
    fputs(str,f);
    fclose(f);

    ch = commonestLetter("input.txt");

}

我猜它与 for 循环中的代码有关,但还没有意识到它有什么问题。

它说 "stack around the Variable ABC was corrupted" 谢谢

看起来你搞砸了声明结构数组。尝试在 commonestLetter 方法中将数组声明 CountABC_t ABC[SIZE] 更改为 CountABC_t ABC[SIZE+5]

解释

您将结构数组声明为 CountABC_t ABC[27],这意味着您可以设置值 ABC[0-26]。但是稍后在 for 循环中,您尝试设置 ABC[27].c 哪个索引大于 26,这样就可以得到 run time error. 要解决此问题,您需要声明大小 > 27 的数组。