【发布时间】:2020-08-14 16:53:22
【问题描述】:
我想在我的链表中搜索等于 x 的字符串(函数搜索的参数)并返回它的值,但是每次函数返回“不存在”而我的列表中的 idf 正常存在。
我认为问题在于“if 语句”不起作用,我不知道如何解决它。
我的代码
typedef struct idf One;
struct idf {
char *idf;
int value;
One *next;
};
typedef struct List_symb {
One *head;
} List ;
int search(List T, char *x){
One *p = T.head;
while(p->next != NULL){
if(p->idf == x){
return p->value;
}
p = p->next;
}
printf("doesn't exist\n");
exit(1);
}
【问题讨论】:
-
您需要使用
strcmp。 -
你的意思是我使用 : if(strcmp(p->idf) == x)? @kiranBiradar
-
@Jo101 strcmp 有两个参数
-
您似乎从未检查过列表的最后一个节点。
-
@Jo101 不,
if (strcmp(p->idf,x) == 0)
标签: c search linked-list singly-linked-list c-strings