views:

221

answers:

5

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.

+2  A: 

Remove the leading '*' of firstMeeting:

missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
tur1ng
A: 

Ok, you need to put the return values functions into a variable. I write C++ for a living my boss would never accept code like this. This code is very difficult to read and even harder to debug. The reason why is because you can setup watches for a variable. You can also see how the program is chaining as you step though the program line by line. A debugger will list all variables in your name space and their corresponding values. I bet money that while you are rewriting this to be more readable, you'll figure out the problem your self.

Rook
+1  A: 

Ok, so the problem is the following, you iterate over registrants but your for stops when it's proccessed attendeeCount items. And also, if missedFirstMeeting is a char*, do as tur1ng said, you need to remove the leading *. So just do this:

for (int i = 0; i < registrantCount; i++) {
    missedFirstMeeting = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
    /* Code that uses missedFirstMeeting here */
}

Edit: If you want to keep all of SearchList return values then you should do something like this:

char** missedFirstMeething = malloc(sizeof(char*)*registrantCount);
for (int i = 0; i < registrantCount; i++) {
    missedFirstMeeting[i] = SearchList((char *)registrants[i], (char **)attendees, attendeeCount);
}

Of course, after you finish using missedFirstMeeting you should free the memory allocated.

Juan
I thought I had to use the * to say that I want the value of missedFirstMeeting to be set to the return value of SearchList, but I guess that is incorrect. But in doing it this way with a for loop, does missedFirstMeeting only get a pointer to one element at a time? And if I did something like missedFirstmMeeting[i], then it would be an array of values?
Crystal
I edited my answer to show how you would do to store all the results in array.
Juan
A: 
mctylr
Compare is correct. `bsearch` looks over a `char**` variable so when it wants to compare key to the i-th element, it will do `Compare(key, haystack+i*size)`, thus passing on a `char**` not a `char*` in both arguments.
Juan
`Compare` isn't needed in the first place. OP can just pass `strcmp` to `qsort` and `bsearch`.
qrdl
@Juan, you are correct, thank you.
mctylr
A: 

The problem is that although computers are big blocks of mutable memory, that's not the best way to imagine them when programming them. You modeled the computer that way, and it didn't quite work out.

anonymous coward