Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C代码-分段错误:11_C_Segmentation Fault - Fatal编程技术网

C代码-分段错误:11

C代码-分段错误:11,c,segmentation-fault,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];

我花了一个小时试图纠正这个错误,但仍然无法解决。我将分段错误精确地放在适当的位置,在这里我不使用数组进行操作

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

您没有初始化
i
j
。在进入
while
循环之前,将两者都设置为0。

以:
char-nch-->
intnch
i
j
,但从未定义。(宽度和高度都不是)首先,
getchar()
返回一个
int
,而不是
char
,如果我没有记错,在这里隐式转换为
int
将导致
EOF
比较失败。调试器告诉了您什么?使用所有警告和调试信息编译(
gcc-Wall-Wextra-g
)。然后使用调试器(
gdb
),哦,对不起,它不是完整的代码,它只是代码的一部分,声明了变量。对不起,我应该上传完整的代码。我初始化它们。