C语言,分段错误在哪里?
这是我得到分段错误的函数C语言,分段错误在哪里?,c,function,segmentation-fault,strcmp,C,Function,Segmentation Fault,Strcmp,这是我得到分段错误的函数 void searchcity() { struct city *ptr=citylist; printf("Which city would you like me to search?: "); scanf("%s",searchedcity); // printf("%s",searchedcity); while(ptr) { if(!strcmp(searchedcity,ptr->nam
void searchcity()
{
struct city *ptr=citylist;
printf("Which city would you like me to search?: ");
scanf("%s",searchedcity);
// printf("%s",searchedcity);
while(ptr)
{
if(!strcmp(searchedcity,ptr->name))
printf("name= %s, statecode = %s,population = %s,region = %s,zipcode = %s\n",ptr->name,ptr->statecode,ptr->population,ptr->region,ptr->zipcode);
else
printf("sorry, couldnt find that city");
ptr=ptr->next;
}
}
不确定是什么原因导致这种情况发生。根据该代码(a),您至少需要检查以下内容:
有足够的空间用于输入(b)searchedcity
链接列表中的所有字符串都已正确构造(以null结尾)citylist
- 结构中的所有字段实际上都是字符数组(或等效指针),而不是整数(例如,填充)
- 列表本身是否正确构建(没有悬空指针或无效指针)
“抱歉,找不到该城市”
,因此,如果您有纽约
、莫斯科
和伦敦
,并且您查找伦敦
,则会在找到该消息之前打印两次
更好的解决方案(多种变体之一)如下:
struct city *ptr = citylist;
while (ptr != NULL)
if (strcmp (searchedcity, ptr->name) == 0)
break;
if (ptr == NULL)
printf ("Sorry, couldnt find that city.\n");
else
printf ("%s, state = %s, pop = %s,r egion = %s, zip = %s\n",
ptr->name, ptr->statecode, ptr->population, ptr->region, ptr->zipcode);
这样,循环负责找到正确的指针或将其设置为NULL。循环后是决定打印内容的正确时间
(a) 除了危险的
scanf
之外,该代码本身似乎还可以,但它确实依赖于许多其他未显示的内容
(b) 事实上,
scanf
具有无限的%s
是代码中的一个严重漏洞,很容易导致缓冲区溢出。有关详细信息和解决方案,请参阅。在任何情况下,scanf(“%s”)
都不是扫描带有空格的字符串的好方法,因为类似于Los Angeles
的东西最终会变成Los
:-)下面的代码有效,我做了一些小的更改,您可以浏览一下。你们犯的几个小错误是你们的ptr->next由于缺少括号而从未执行过。剩下的我都写在代码里了
谢谢,希望我们能帮忙
#include <stdio.h>
struct city {
char name[100];
char statecode[100];
char population[100];
char region[100];
char zipcode[100];
struct city* next;
};
int main() // this is your searchcity function
{
char searchedcity[100]; // make sure you initialize this. YOu haven't done it in the code you gave us.
// Assume citylist is in your main function or initialized as a global var
// I initialized it here for simplicity
struct city* citylist = (struct city*) malloc(sizeof( struct city));
strcpy(citylist->statecode,"statecode");
strcpy(citylist->population,"population");
strcpy(citylist->region,"region");
strcpy(citylist->zipcode,"zipcode");
citylist->next = NULL;
//end of citylist
struct city *ptr = citylist;
printf("Which city would you like me to search?: ");
scanf("%s",searchedcity);
// printf("%s",searchedcity);
while(ptr) {
printf("while \n");
if(!strcmp(searchedcity,ptr->name)){
printf("name= %s, statecode = %s,population = %s,region = %s,zipcode = %s\n",ptr->name,ptr->statecode,ptr->population,ptr->region,ptr->zipcode);
}else{
printf("sorry, couldnt find that city");
ptr=ptr->next;
}
}
return 0;
}
#包括
结构城市{
字符名[100];
字符状态码[100];
字符数[100];
半焦区[100];
char-zipcode[100];
结构城市*下一步;
};
int main()//这是您的searchcity函数
{
char searchedcity[100];//请确保对此进行初始化。您没有在提供给我们的代码中进行初始化。
//假设citylist位于主函数中或初始化为全局变量
//为了简单起见,我在这里初始化了它
struct city*citylist=(struct city*)malloc(sizeof(struct city));
strcpy(citylist->statecode,“statecode”);
strcpy(城市列表->人口,“人口”);
strcpy(城市列表->区域,“区域”);
strcpy(城市列表->zipcode,“zipcode”);
citylist->next=NULL;
//城市列表末尾
struct city*ptr=城市列表;
printf(“您希望我搜索哪个城市?:”;
scanf(“%s”,搜索城市);
//printf(“%s”,搜索城市);
while(ptr){
printf(“while\n”);
如果(!strcmp(searchedcity,ptr->name)){
printf(“名称=%s,状态代码=%s,填充=%s,区域=%s,zipcode=%s\n”,ptr->name,ptr->statecode,ptr->population,ptr->region,ptr->zipcode);
}否则{
printf(“对不起,找不到那个城市”);
ptr=ptr->next;
}
}
返回0;
}
原因可能有很多,但是如果没有更多的代码,很难判断这里没有足够的代码来调试它。但是函数中有什么原因导致它吗?@alex我们需要看到更多的代码来解决这个问题,而不需要猜测。你有没有用调试器来验证这一点,看看是否有任何指针是NULL
但不应该是空的?请解释原始代码中未执行ptr->next
的争用。在大括号内移动它对循环没有影响,因为如果它为NULL,它就不会在循环中。@paxdiablo哦,它有一个效果好吧。。。当城市被发现时,它会产生一个无限循环。循环是错误的,但这肯定不是解决办法。