tags:

views:

226

answers:

1

I'm having trouble with some of the pointer/array notation used. I have two lists and am sorting them, and then try to display them. I had 3 comments in my code below as to what the declarations are and why. My code looks like:

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));
}

/*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; // not sure if this is the right declaration
    char *start, *end;

    // not sure if this is right with the &attendees and registrants for the bsearch()
    missedFirstMeeting = bsearch(&attendees, registrants, attendeeCount, 
                                 sizeof(attendees[0]), Compare);
    printf("Missed First Meeting: \n");

   //not sure if this the way to traverse through the array using pointers to display
    for (start = missedFirstMeeting, end = &missedFirstMeeting[registrantCount-1]; start < end; ++start) {
        printf("%s", *start);
    }
}
+1  A: 

This appears to be homework, so I'll answer in such a way as to (hopefully) lead you in the correct direction.

The bsearch() function searches for one element in a sorted list, and returns either its location, or an indicator saying it wasn't found. Your code as posted above appears to be using bsearch() in a different way.

Consider treating each registrant individually, and using bsearch() more than once to see whether each registrant is in the attendees list. If not, then show the registrant name. Don't forget that bsearch() only works correctly if the list is sorted.

Greg Hewgill