Reputation: 83
I am using bsearch with a key value equivalent to an array element thats value is a pointer. The key is the elements of an array thats an array of character pointers. I dont think you can dereference array element value by indexing and use value as pointer to char string. I tried casting element value to (char *) but that did not work. Im getting garbage for return value of 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]);
}
}
Upvotes: 0
Views: 203
Reputation: 18399
Since input array is of type const char* []
, return value of bsearch
would be const char**
. Here is your modified code:
#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;
}
Last loops are removed since it have a format error (as pointed in comment by WhozCraig) and I'm not really grasp what it meant to do, and it isn't relevant to the question. Please also note that you didn't free memory that you've allocated with malloc
.
For possible future questions, please include complete use case whenever possible (somehting like what I've made up in main
function).
Upvotes: 1