I've been trying to solve this bsearch homework problem for awhile now. I try using my code to first search for one entry like so:
int Compare(const void *a, const void *b);
void SortStudents(char *studentList[], size_t studentCount)
{
qsort(studentList, studentCount, sizeof(studentList[0]), Compare);
}
int Compare(const void *a, const void *b)
{
return (strcmp(*(char **)a, *(char **)b));
}
char *SearchList(char *key, char *list[], size_t num)
{
char **value = bsearch(&key, list, num, sizeof(list[0]), Compare);
return (value == 0 ? 0 : *value);
}
/*Determines which registrants did not attend the first meeting by searching for registrants
that are not in attendees set. */
void DisplayClassStatus(
const char *registrants[], size_t registrantCount,
const char *attendees[], size_t attendeeCount)
{
char *missedFirstMeeting = SearchList((char *)registrants[0], (char **)attendees, attendeeCount);
}
My missedFirstMeeting seems to work in calling out a single value properly, but when I try to repeatedly call my SearchList function in a loop like so:
for (int i = 0; i < attendeeCount; i++) {
*missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
}
I get a segmentation fault error. To me it seems like I am doing the same thing, but just repeatedly calling the SearchList(), but obviously something is wrong that I do not see since I get that segmentation fault error. Any ideas? Thanks.