0

我正在使用 bsearch,其键值相当于一个数组元素,其值是一个指针。关键是一个数组的元素,它是一个字符指针数组。我认为您不能通过索引来取消引用数组元素值并将值用作指向 char 字符串的指针。我尝试将元素值转换为 (char *) 但这不起作用。我正在为 bsearch 的返回值获取垃圾。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int Compare(const void *elemA, const void *elemB){
 return strcmp(*(char **)elemA, *(char **)elemB);
}


void SortStudents(const char *studentList[], size_t studentCount){

qsort(studentList, studentCount, sizeof(studentList[0]), Compare);

}

void DisplayClassStatus(const char *registrants[], size_t registrantCount,
const char *attendees[], size_t attendeeCount) {


int counter;
int *regnotattend_status = (int *)malloc(sizeof(int) * registrantCount);
int *attendeenotreg_status = (int *)malloc(sizeof(int) * attendeeCount);
char *attendeeStatus, *registrantstatus;

for ( counter = 0; counter < (int)registrantCount; counter++) {
  attendeeStatus = (char *) bsearch(&registrants[counter], attendees,              
attendeeCount, sizeof(attendees[0]), Compare);

  if (attendeeStatus == NULL)
     regnotattend_status[counter] = 0;
  else{
     regnotattend_status[counter] = 1;
     printf(" attendeestatus = %s \n", attendeeStatus);
  }
} 

for (counter = 0; counter < (int)attendeeCount; counter++){
  registrantstatus = (char *)bsearch(&attendees[counter], registrants,     
registrantCount, sizeof(registrants[0]), Compare);
  if ( registrantstatus == NULL)
     attendeenotreg_status[counter] = 0;
  else
     attendeenotreg_status[counter] = 1;
  printf("registrantstatus = %s \n", registrantstatus);
 } 

printf(" Not present: \n");
for ( counter = 0; counter < (int)registrantCount; counter++) {
  if (regnotattend_status[counter] == 0)
     printf(" %s \n", registrants[counter]);
}
printf( "\n");
printf(" Not registered: \n");
for ( counter = 0; counter < (int)attendeeCount; counter++) {
  if (attendeenotreg_status[counter] == 0)
     printf(" %s  \n", attendeenotreg_status[counter]);
} 
}
4

1 回答 1

1

由于输入数组的类型为,因此const char* []返回值为。这是您修改后的代码:bsearchconst char**

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int Compare(const void *elemA, const void *elemB){
    return strcmp(*(char **)elemA, *(char **)elemB);
}


void SortStudents(const char *studentList[], size_t studentCount){

    qsort(studentList, studentCount, sizeof(studentList[0]), Compare);

}

void DisplayClassStatus(const char *registrants[], size_t registrantCount,
        const char *attendees[], size_t attendeeCount) {


    int counter;
    int *regnotattend_status = (int *)malloc(sizeof(int) * registrantCount);
    int *attendeenotreg_status = (int *)malloc(sizeof(int) * attendeeCount);
    char **attendeeStatus, **registrantstatus;

    for ( counter = 0; counter < (int)registrantCount; counter++) {
        attendeeStatus = (char **) bsearch(&registrants[counter], attendees,              
                attendeeCount, sizeof(attendees[0]), Compare);

        if (attendeeStatus == NULL)
            regnotattend_status[counter] = 0;
        else{
            regnotattend_status[counter] = 1;
            printf(" attendeestatus = %s \n", *attendeeStatus);
        }
    } 

    for (counter = 0; counter < (int)attendeeCount; counter++){
        registrantstatus = (char **)bsearch(&attendees[counter], registrants,     
                registrantCount, sizeof(registrants[0]), Compare);
        if ( registrantstatus == NULL)
            attendeenotreg_status[counter] = 0;
        else {
            attendeenotreg_status[counter] = 1;
            printf("registrantstatus = %s \n", *registrantstatus);
        }
    } 
}

int main(int argc, char **argv) {
    (void)argc, (void)argv;

    const char *attendies[] = {
        "foo", "bar", "foobar"
    };

    const char *registrants[] = { "bar" };

    SortStudents(attendies, 3);
    DisplayClassStatus(registrants, 1, attendies, 3);

    return 0;
}

最后一个循环被删除,因为它有一个格式错误(正如 WhozCraig 在评论中指出的那样),我并没有真正理解它的含义,它与问题无关。另请注意,您没有释放已分配的内存malloc

对于未来可能出现的问题,请尽可能包含完整的用例(类似于我在main函数中编写的内容)。

于 2015-08-09T16:42:26.433 回答