Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言实现链表中的字符串搜索_C_Linked List - Fatal编程技术网

用C语言实现链表中的字符串搜索

用C语言实现链表中的字符串搜索,c,linked-list,C,Linked List,在if条件的while循环中面临问题, 条件值相等,但在if条件中不进入内部 void searchList(char name[20]) { char contactName[20]; strcpy(contactName,name); struct node *temp = head; printf("\nSearch Contact : \n"); printf("-------------------

在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) {
     ...
 }
#包括
...
如果(strcmp(临时->名称,联系人名称)==0){
...
}

请参阅此处的更多信息

使用strcmp比较字符串,相等字符串名称在C中不起作用。字符串名称实际上表示第一个位置的地址,因此您实际上是在比较地址而不是字符串。使用
strcmp
比较C中的字符串。例如
if(temp->name==contactName)
-->
if(strcmp(temp->name,contactName)==0)
不应该是
==0
吗?你的意思是
==
而不是
!=
。对吗?是的。抱歉,仓促键入…使用strcpy()有库依赖性吗?你需要在代码中使用“#inculde”头文件