gpt4 book ai didi

c - 无限循环(strstr)

转载 作者:行者123 更新时间:2023-11-30 16:07:42 27 4
gpt4 key购买 nike

由于 strstr 函数,我在该方法内有一个无限循环。是因为我将结构类型与字符类型相匹配吗?

car* find(char* type){

car* stringCurr = list_head();
while(stringCurr != NULL){
if(strstr(stringCurr->info, type)){ //This line
return stringCurr;
}
}
return NULL;
}

结构:

typedef struct Machine{

char info[128];
struct Machine* next;

}car;

编辑:

目标是创建一个搜索函数,如果在列表中找到对象则返回 NULL

最佳答案

您错过了递增字符串 Curr。您需要在 while 循环中执行类似于 string Curr = stringCurr->next 的操作。

car* find(char* type){

car* stringCurr = list_head();
while(stringCurr != NULL){
if(strstr(stringCurr->info, type)){ //This line
return stringCurr;
}
stringCurr = stringCurr -> next;
}
return NULL;
}

关于c - 无限循环(strstr),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59554305/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com