[英]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.