2010-02-20 2 views
2

사용 된 포인터/배열 표기법에 문제가 있습니다. 나는 두 개의 목록을 가지고 있고 그것들을 정렬하고 표시하려고 시도한다. 선언문의 의미와 이유에 대해서는 아래 코드에서 3 가지 설명을 들었습니다. 같은 내 코드는 모양이 숙제로 나타납니다, 그래서하는 방식으로 답변 해 드리겠습니다qsort, bsearch를 사용하는 C의 포인터 도움말

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

숙제 인 경우 '숙제'태그를 추가하십시오. –

답변

1

(희망) 올바른 방향으로 당신을 이끌.

bsearch() 함수는 하나의 정렬 된 목록에서 요소를 검색하고 해당 위치 또는 찾을 수 없음을 나타내는 표시기를 반환합니다. 위에 게시 된 코드는 다른 방식으로 bsearch()을 사용하는 것 같습니다.

각 등록자를 개별적으로 처리하고 bsearch()을 두 번 이상 사용하여 각 등록자가 참석자 목록에 있는지 확인하십시오. 그렇지 않은 경우 등록자 이름을 표시하십시오. bsearch()은 목록이 정렬되어있는 경우에만 올바르게 작동한다는 것을 잊지 마십시오.