2010-02-20 66 views
2

我遇到了一些使用的指針/數組符號的問題。我有兩個列表並將它們排序,然後嘗試顯示它們。我在下面的代碼中有3條註釋,說明聲明是什麼以及爲什麼。我的代碼如下所示:C中使用qsort指針的幫助,bsearch

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

如果這是作業,請添加'作業'標籤。 – 2010-02-20 23:17:47

回答

1

這似乎是家庭作業,所以我會以這樣的方式回答,因爲(希望)帶領你在正確的方向。

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

考慮分別處理每個註冊人,並多次使用bsearch()以查看每個註冊人是否在與會者列表中。如果沒有,則顯示註冊人名稱。不要忘記bsearch()只有在列表排序時才能正常工作。