自己运行的时候没有问题,但是在oj上显示运行错误,原因有两种可能:
1、使用了系统禁止的操作系统调用,看看是否越权访问了文件或进程等资源。
2、缓冲区溢出,检查是否有字符串长度超出数组的情况。
请问能帮我看看怎么改吗
#include
#include
#include
int main()
{
int n,i,j;
scanf("%d",&n);
char *str[100],temp[80],*t;
for(i=0;iscanf("%s",temp);
str[i]=(char *)malloc(80*sizeof(char));
strcpy(str[i],temp);
}
t=str[0];
for(j=0;jif(strlen(t)<strlen(str[j]))
t=str[j];
}
printf("The longest is: %s",t);
return 0;
}
你先把原题放出来
每段代码都是针对特定需求的
不说需求谁知道你的代码在什么特定环境下会出问题
问题:没有释放内存。
修改:
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int n,i,j;
scanf("%d",&n);
char *str[100],temp[80],*t;
for(i=0;i<n;i++)
{
scanf("%s",temp);
str[i]=(char *)malloc(80*sizeof(char));
strcpy(str[i],temp);
}
t=str[0];
for(j=0;j<n;j++)
{
if(strlen(t)<strlen(str[j]))
t=str[j];
}
printf("The longest is: %s",t);
for(i=0;i<n;i++)
{
free(str[i]);
}
return 0;
}