在权重[a]= data上的C段故障;


fscanf(ifile, "%c", &jnk);
/* If there's a height, this will be a ". if not, it'll be a ,*/
if ((jnk = '"') || (jnk != ',')) {
    printf("%c", jnk);
    while(fscanf(ifile, "%c", &data) && data != ',' && data != '"' && data != ''') {
        printf(" %c ", data);
        weight[a]= data;
        a++;
    }
    weight[a] = '';
    player[n].weight=atof(weight);
    printf("%.0f ", player[n].weight);
} else if(data == ' ' || data == ',') {
    player[n].weight= 0;
    printf("%.0f ", player[n].weight);
}

GDB告诉我将数据存储到权重中,导致segfault。有人能告诉我为什么吗?

问题就在这里:

我希望你把",分配给a。当你使用weight[a]时,你会得到一个错误,因为字符的ASCII值被取走了,这可能超出了你声明的数组大小的范围

我认为问题在这里

fscanf(ifile, "%c", &data)

 printf(" %c ", data);

根据数据的数据类型,你解释这个变量的方式有问题,也取决于你定义的不同变量的初始值是什么。

最新更新