我很高兴知道为什么我遇到此错误 http://www.codechef.com/problems/AXR1P2在 codechef.com 中,我的代码是...
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *num=0,n=0,i=0,max=0;char *s="";
int sum[9]={1,5,14,20,25,31,32,38,39},dsum[9]={1,7,8,14,19,25,26,32,33},unitdig=0;
do
{
gets(s);
*(num+i)=(int)atoi(s);
i++;
}while(*s!='#');
max=i;
for(i=0;i<max-1;i++)
{
n=*(num+i);
if(n>10)
unitdig=33*(n/10-1)+39+dsum[(n%10)-1];
else
unitdig=sum[(n%10)-1];
printf("%d\n",unitdig%10);
}
getchar();
return 0;}
最佳答案
几个错误:
char * s = "";
...
gets(s);
您正在尝试读取字符串文字,您想要:
char s[SOMESIZE];
并且您还没有初始化 num 来指向任何内容,因此:
*(num+i)=(int)atoi(s);
您正在取消引用空指针。可能还有其他问题 - 这是我(几乎)立即发现的两个问题。
关于c - 运行时错误(SIGSEGV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2689809/