首页 文章

C代码 - 分段错误:11

提问于
浏览
-1

我试图修复这个错误一个小时,但仍然无法弄明白 . 我准确地得到了分段错误,我没有使用数组进行操作 .

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;
        }
    }

1 回答

  • 1

    你没有初始化 ij . 在进入 while 循环之前将两者都设置为0 .

相关问题