C中的Seg fault on weight[a]= data;

xvw2m8pv  于 2023-03-22  发布在  其他
关注(0)|答案(2)|浏览(117)
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] = '\0';
    player[n].weight=atof(weight);
    printf("%.0f ", player[n].weight);
} else if(data == ' ' || data == ',') {
    player[n].weight= 0;
    printf("%.0f ", player[n].weight);
}

广发银行告诉我把数据存入权重给予我一个分段错误。谁能告诉我为什么?

tkclm6bt

tkclm6bt1#

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

3xiyfsfu

3xiyfsfu2#

我觉得问题要么在这里

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

printf(" %c ", data);

根据数据的数据类型,您解释此变量的方式可能存在错误,并且还取决于您定义的不同变量的初始值。

相关问题