This question already has an answer here:
这个问题已经有了答案:
- How to compare strings in an “if” statement? 5 answers
- 如何在“if”语句中比较字符串?5个回答
I'm building a linked list that builds/tracks a movie database. In my search function I have an if statement that checks if the pointer to the title of the movie is equal to the inputted value in the search function. However, the if statement does not run even though the values are equal to each other. I didn't want to include all of my code so I included both input fields for the if statement and the loop the if statement sits in. I have validated that the values of movieTitle
and ptr->title
are lexically the same.
我正在构建一个建立/跟踪一个电影数据库的链表。在我的搜索函数中,我有一个if语句,它检查是否指向电影标题的指针是否等于搜索函数中输入的值。然而,即使值相等,if语句也不运行。我不想包含所有的代码,所以我在if语句中包含了两个输入字段以及if语句所在的循环。我已经验证了电影标题和ptr->标题的价值在词汇上是一样的。
ptr->title input
ptr - >标题输入
printf("Name of the Movie: ");
scanf(" %[^\n]%*c", m.title);
strcpy(ptr->title, m.title);
movieTitle input
movieTitle输入
printf("Name of the Movie: ");
scanf(" %[^\n]%*c", movieTitle);
If Statement
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;
}
}
1 个解决方案
#1
3
You can't use == to compare two strings in C
你不能用==来比较C中的两个字符串。
using strcmp
使用比较字符串
Something like this
像这样的东西
if (strcmp( string1, string2) == 0)
#1
3
You can't use == to compare two strings in C
你不能用==来比较C中的两个字符串。
using strcmp
使用比较字符串
Something like this
像这样的东西
if (strcmp( string1, string2) == 0)