运行时错误 - C 编程,IDE 上没有错误
Runtime Error - C Programming , No Error on IDE
这里是问题的描述https://a2oj.com/p?ID=193,在Visual Studio上运行的很好,但是不知为何会出现Runtime - Error on the website's online judge compiler,我很难检测到因为他们的编译器没有告诉是什么测试用例产生了错误。
#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];
您忘记包含终止符 '[=15=]'
与运行时错误无关,但可能仍然是无意的:
max = -1;
char result = NULL;
memset(count,0,sizeof(count));
这里定义第二个 result
隐藏前面的定义。
也可能与您的问题无关:
void main()
不正确。成功
int main(void)
或
int main (int argv, char *argc[])
和 return 0 表示成功。
这里是问题的描述https://a2oj.com/p?ID=193,在Visual Studio上运行的很好,但是不知为何会出现Runtime - Error on the website's online judge compiler,我很难检测到因为他们的编译器没有告诉是什么测试用例产生了错误。
#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];
您忘记包含终止符 '[=15=]'
与运行时错误无关,但可能仍然是无意的:
max = -1;
char result = NULL;
memset(count,0,sizeof(count));
这里定义第二个 result
隐藏前面的定义。
也可能与您的问题无关:
void main()
不正确。成功
int main(void)
或
int main (int argv, char *argc[])
和 return 0 表示成功。