[英]Runtime Error - C Programming , No Error on IDE
这是问题的描述https://a2oj.com/p?ID=193 ,它在Visual Studio上工作得很好但是由于某种原因它会在网站的在线判断编译器上产生运行时错误,我很难检测到它因为他们的编译器没有告诉测试用例产生了什么错误。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ASCII_SIZE 255
void main(){
int testCases;
char caseInput[100];
int count[ASCII_SIZE] = {0};
int strLength;
int max = -1;
char result = NULL;
typedef struct occurrence{
int numOfOcc;
char occLetter; }Occurence;
scanf("%d",&testCases);
Occurence *ptr;
ptr = (Occurence*)malloc(testCases * sizeof(Occurence));
if (ptr){
for (int caseValue = 0; caseValue < testCases; caseValue++)
{
scanf("%s",caseInput);
strLength = strlen(caseInput);
for (int i=0; i<strLength; i++)
count[caseInput[i]]++;
for (int i = 0; i < strLength; i++) {
if (max <= count[caseInput[i]]) {
if (result > caseInput[i] && i > 0 ){
max = count[caseInput[i]];
result = caseInput[i];
}
else if ( i == 0 ){
max = count[caseInput[i]];
result = caseInput[i];
}
}
}
ptr[caseValue].numOfOcc = max;
ptr[caseValue].occLetter = result;
max = -1;
char result = NULL;
memset(count,0,sizeof(count));
}
for (int i = 0; i < testCases; i++)
{
printf("%d %c\n",ptr[i].numOfOcc,ptr[i].occLetter);
}
}
}
您不能在缓冲区中存储最多包含100个字母的输入字符串:
char caseInput[100];
你忘了包含终止'\\0'
与运行时错误无关但可能仍然无意中:
max = -1;
char result = NULL;
memset(count,0,sizeof(count));
在这里,您可以定义隐藏先前定义的第二个result
。
也可能与您的问题无关:
void main()
是不正确的。 做了
int main(void)
要么
int main (int argv, char *argc[])
并在成功时返回0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.