C代码 - 分段错误:11

时间:2015-12-06 15:53:32

标签: c segmentation-fault

我正在尝试将此错误修复一个小时,但仍然无法弄明白。我准确地得到了分段错误,我不用数组操作。

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 个答案:

答案 0 :(得分:1)

您未初始化ij。在进入while循环之前将它们都设置为0。