簡體   English   中英

使用qsort,bsearch幫助C中的指針

[英]Help with pointers in C using qsort, bsearch

我在使用某些指針/數組符號時遇到了麻煩。 我有兩個列表,正在對其進行排序,然后嘗試顯示它們。 我在下面的代碼中對聲明是什么以及原因有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);
    }
}

這似乎是家庭作業,所以我將以一種希望的方式(希望)將您引向正確的方向。

bsearch()函數在排序列表中搜索一個元素,然后返回其位置或指示未找到該元素的指示符。 上面發布的代碼似乎以不同的方式使用bsearch()

考慮單獨對待每個注冊人,並多次使用bsearch()來查看每個注冊人是否在與會者列表中。 如果不是,則顯示注冊人名稱。 不要忘記bsearch()僅在列表排序后才能正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM