2013-04-15 51 views
-1

这里是即时得到分段错误C语言,分段错误在哪里?

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; 
    } 
} 

不知道什么可以导致这种情况发生的功能。

+5

可能有很多原因,但没有更多的代码很难说 –

+1

这里没有足够的代码来调试这个。 – Yuushi

+0

有没有什么可以导致它在函数中呢? – Alex

回答

0

基于代码(一),这里就是你需要检查,以最低的是什么:

  • searchedcity对投入足够的空间(B)
  • citylist链接列表中保存的所有字符串都被正确构造(以空值终止)。
  • 结构中的所有字段实际上都是字符数组(或等效指针),而不是整数(例如总体)。
  • 该列表本身是正确构建的(没有悬挂或无效的指针)。

你确实有其中一个其他问题,虽然与segfault无关。

你的代码将打印"sorry, couldnt find that city"节点列表,它不是你的城市相匹配的话,如果你有New YorkMoscowLondon,你找London,你会得到的消息打印前两次它发现它。

更好的解决方案(许多变种之一)会是这样的:

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。 之后,循环是决定应该打印什么的正确时间。


(一)该代码本身似乎比危险scanf好等,但它不依赖于相当未显示很多其他的东西。

(B)事实上,scanf采用无界%s是在你的代码中的严重漏洞,可能容易导致缓冲区溢出。详情请见here。在任何情况下,scanf("%s")是不扫描带空格的字符串,因为像Los Angeles最终会为Los :-)以下作品

-1

代码,我也可以步行通过细微的变化的好办法。你有几个小错误是你的ptr->下一个从未执行,因为缺少括号。我在代码中写的其余部分。

感谢希望我们的帮助。

#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; 
} 
+1

请解释在原始代码中未执行'ptr-> next'的争用。将它移动到大括号中对循环没有影响,因为如果它是NULL,它不会被放入循环中。 – paxdiablo

+2

@paxdiablo哦,它有一个好的效果......当城市被发现时它会产生一个无限循环。循环是错误的,但这肯定不是修复。 –