gpt4 book ai didi

c - 迭代链表,if 语句不起作用

转载 作者:行者123 更新时间:2023-11-30 18:40:09 26 4
gpt4 key购买 nike

我正在构建一个链接列表来构建/跟踪电影数据库。在我的搜索函数中,我有一个 if 语句,用于检查指向电影标题的指针是否等于搜索函数中输入的值。但是,即使值彼此相等,if 语句也不会运行。我不想包含所有代码,因此我包含了 if 语句的输入字段和 if 语句所在的循环。我已经验证了 movieTitle 的值。和ptr->title在词汇上是相同的。

ptr->标题输入

printf("Name of the Movie: ");
scanf(" %[^\n]%*c", m.title);
strcpy(ptr->title, m.title);

电影标题输入

printf("Name of the Movie: ");
scanf(" %[^\n]%*c", movieTitle);

If 语句

while (ptr != NULL)
{
if (movieTitle == ptr->title)
{
printf("Year: %d\n", ptr->year);
printf("Rating: %hhu\n", ptr->rating);
found = true;
break;
}
else
{
tmp = ptr;
ptr = ptr->next;
}
}

最佳答案

在 C 中不能使用 == 来比较两个字符串

使用 strcmp

类似这样的事情

if (strcmp( string1, string2) == 0) 

关于c - 迭代链表,if 语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27071241/

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