在 if 条件的 while 循环中面临问题, 条件值相等但不进入 if 条件。
void searchList(char name[20])
{
char contactName[20];
strcpy(contactName,name);
struct node *temp = head;
printf("\nSearch Contact : \n");
printf("-------------------\n");
printf("Name : %s\n",name);
while (temp != NULL)
{
if(temp->name == contactName)
{
printf("Contact Name : %s\n",temp->name);
printf("Contact Number : %s\n", temp->phone);
}
temp = temp->next;
}
}
最佳答案
您应该使用 string.h 库中的 strcmp 函数来比较字符串:
#include <string.h>
...
if (strcmp(temp->name, contactName) == 0) {
...
}
关于c - C在链表中查找字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45273207/