[英]Segmentation Fault (core dumped) when using strcmp
I am making an application that registers client's cars and has some filtering options.我正在制作一个注册客户汽车并具有一些过滤选项的应用程序。 I was able to do almost all of them with ease, but the filtering by model just won't work.我几乎可以轻松完成所有这些操作,但是 model 的过滤不起作用。 If someone could guide me on what I'm doing wrong it would be much appreciated.如果有人可以指导我做错了什么,将不胜感激。
Complete code in gitlab: https://gitlab.com/salgadoth/car-ownership-in-c/-/blob/master/carownership.c gitlab 中的完整代码: https://gitlab.com/salgadoth/car-ownership-in-c/-/blob/master/carownership.Z4A8A08F09D37B737956490384
void listModel()
{
int i, comp;
char model[20];
printf("\nTYPE IN DESIRED MODEL:\n");
fflush(stdin);
scanf("%s", &model);
printf("DESIRED MODEL: %s\n\n", model);
for(i = 0; i < counter; i++)
{
if(strcmp(model, car[i].model) == 0)
{
printf("\nBRAND: %s", car[i].brand);
printf("\nMODEL: %s", car[i].model);
printf("\nLICENSE PLATE: %s", car[i].license_plate);
printf("\nYEAR OF FABRICATION: %d\n", car[i].year);
printf("---------------------------\n");
}
else
{
printf("\nERROR - NO CAR FOUND");
return 1;
}
}
}
You never initialize counter
, so nothing stops your for
loop from running into objects you didn't initialize.您永远不会初始化counter
,因此没有什么可以阻止您的for
循环运行到您未初始化的对象中。 If model
doesn't contain a valid string, then strcmp
can fault.如果model
不包含有效字符串,则strcmp
可能会出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.