我正在使用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(®istrants[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]);
}
}
答案 0 :(得分:1)
由于输入数组的类型为const char* []
,因此bsearch
的返回值为const 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(®istrants[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
函数中所做的那样)。