检查是否为(char =='\n')时出现分段错误

Geo*_*off -1 c segmentation-fault getchar

我一直在努力解决这个问题超过一个小时,我似乎无法找出为什么会出现这个错误.

int  inp, count;
char numBuff[21];

count = 0;
while((inp=getchar()) != EOF) { // get Value (last field)
    printf("input is '%c'\n", inp);
    if (inp == '\n') break;
    if (inp == ' ') {
        continue;
    }
    numBuff[count++] = inp;
    printf("go back through loop\n");
}
printf("Out!");
numBuff[count] = '\0';
Run Code Online (Sandbox Code Playgroud)

如果我输入1013,我会得到以下内容

input is '1'
go back through loop
input is '0'
go back through loop
input is '1'
go back through loop
input is '3'
go back through loop
input is '
'
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

我唯一可以从中收集的是,当我检查inp =='\n'时它是否失败但是为什么?我通过循环printf移动回到检查后是否inp =='\n'并且它从未到达那个,所以我知道它正在那里发生.

P.P*_*.P. 6

count是未初始化的,用作数组索引.将其初始化为0.