-1

I'm attempting to implement a linear search function for strings in C, but it isn't currently working. Here is my code:

// Linear search for name matching input string

int listSearch(struct LinkedList* linkedList, char name)
{
    struct StudentRecord* temp = linkedList->head; // Go to first item in linked list
    int count = 0; // Count variable to give index of search item

    while((temp != NULL) && (name != temp->name))
    {
        temp = temp->next;
        count++;
    }

    return count;
}

And here is the function call to listSearch:

printf("\nItem: Tim\nIndex: %d", listSearch(list_1, "Tim"));

'Tim' is at index 3, but the output consistently puts him at index 4 (there are 4 total items in the list and thus index 4 doesn't exist) - and the same is true for any item we search for. This leads me to believe that the (name != temp->name) condition is failing, but I can't for the life of me see why...Could anyone give me a hint as to why it isn't working?

Mick McCarthy
  • 428
  • 2
  • 16
  • If it's in C, don't tag your question C++ please. – Rakete1111 Oct 25 '16 at 18:35
  • Righto, will bear that in mind. Any insight into what my problem is here? – Mick McCarthy Oct 25 '16 at 18:36
  • You're automatically skipping your first entry when you assign to `temp`. (`temp = linkedList->head;`) – Jamie Oct 25 '16 at 18:36
  • 1
    The code doesn't make sense. `name` is a char, but it's being passed a string aka char*. – QuestionC Oct 25 '16 at 18:37
  • 1
    You're also just comparing pointers; perhaps you want `while(temp != NULL && temp->name != NULL && strcmp(name, temp->name)`. But fix your function signature first: `char * name` not `char name`. – Jamie Oct 25 '16 at 18:38
  • @Jamie Great answer, thank you! Code now works fine. As for skipping my first entry, surely this doesn't matter as the variable count is declared as 0 which, if the given name is at index 0, will be given as the return value because the while exit condition will immediately be met (if that makes sense!). Feel free to correct me if I'm wrong, but I've just tested for all items and it seems to be working. – Mick McCarthy Oct 25 '16 at 18:48
  • Yes you are right @LouisCowell the function would return 0 in the case you specified – Cherubim Oct 25 '16 at 19:06

2 Answers2

0

You're passing in a char, not a pointer to a char and as a result, you were comparing a char to a string pointer. You also need to compare the strings.

int listSearch(struct LinkedList* linkedList, char * name)
{
    struct StudentRecord* temp = linkedList; // Go to first item in     linked list
    int count = 0; // Count variable to give index of search item

    while(temp != NULL) {
       if (temp->name != NULL && strcmp(name,temp->name)) {
            count++;
       }
       temp = temp->next;

    }

    return count;
}
Jamie
  • 7,075
  • 12
  • 56
  • 86
0

Use strcmp to compare two strings, for example:

if(strcmp(a,b)==0)
     printf("Entered strings are equal");
else
    printf("Entered strings are not equal");
RCanseco
  • 1
  • 1