我正在尝试将此错误修复一个小时,但仍然无法弄明白。我准确地得到了分段错误,我不用数组操作。
int height, width, i=0, j=0;
char newline;
scanf("%d %d%c", &height, &width, &newline);
if(newline != '\n')
{
return 0;
}
char pole[height][width];
char nch;
while(1)
{
nch = getchar();
if(nch == EOF)
{
break;
}
if(nch != '\n')
{
pole[i][j] = nch;
printf("i=%d a j=%d\n", i, j); //for my info, there it still runs
j++;
}
//The end of working piece of code (in last cycle...)
if(j>= width)
{
j=0;
i++;
}
if(i >= height)
{
break;
}
}
答案 0 :(得分:1)
您未初始化i
和j
。在进入while
循环之前将它们都设置为0。