2

使用されているポインター/配列表記の一部に問題があります。2 つのリストがあり、それらを並べ替えてから表示しようとしています。以下のコードには、宣言とは何か、またその理由について 3 つのコメントがありました。私のコードは次のようになります:

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

1 に答える 1

1

これは宿題のようですので、(できれば) 正しい方向に導くような方法でお答えします。

この関数は、並べ替えられたリスト内の1 つのbsearch()要素を検索し、その場所、または見つからなかったことを示すインジケーターを返します。上記のコードは、別の方法で使用しているようです。bsearch()

各登録者を個別に扱い、bsearch()複数回使用して各登録者が出席者リストに含まれているかどうかを確認することを検討してください。そうでない場合は、登録者名を表示します。bsearch()リストがソートされている場合にのみ正しく機能することを忘れないでください。

于 2010-02-20T22:53:41.247 に答える