0

Facing problem in while loop for if condition, conditional values are equal but not going inside in if condition.

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;
        }
    }
Hasib Kamal Chowdhury
  • 2,476
  • 26
  • 28
  • 1
    Use strcmp to compare strings, equating string names doesn't work in C. String name actually represents address of first location, so you are actually comparing addresses and not the strings. – akshayk07 Jul 24 '17 at 05:33
  • Use `strcmp` to compare strings in C. E.g `if(temp->name == contactName)` --> `if(strcmp(temp->name, contactName) ==0)` – BLUEPIXY Jul 24 '17 at 05:33

1 Answers1

1

You should use the strcmp function from string.h library to compare the strings:

 #include <string.h>

 ...

 if (strcmp(temp->name, contactName) == 0) {
     ...
 }

See more info here https://stackoverflow.com/a/8004250/492620

akshayk07
  • 2,092
  • 1
  • 20
  • 32
oz123
  • 27,559
  • 27
  • 125
  • 187