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?