There are two versions of compare_first_character functions, and they are both passed into another function. However, the first would yield correct matches of words while the second would return nothing found despite there being one.
Version 1: [Version 1 of string comparison]
int compare_first_characters(const void *p, const void *q) {
return **(char **)p - **(char **)q;
}
Version 2(fail): [Version 2 of string comparison]
int compare_first_characters(const void *p, const void *q) {
return strcmp(*(char **)p, *(char **)q);
}
Suspected function where problems emerge:
const void *found = bsearch(key, words, n, sizeof(words[0]), compare_first_characters);
Complete program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int compare_first_characters(const void *p, const void *q) {
return strcmp(*(char **)p, *(char **)q);
}
int main(int argc, char *argv[]) {
char *words[] = {"red", "blue", "green", "yellow"};
int n = sizeof(words) / sizeof(*words);
const char *key = "g";
qsort(words, n, sizeof(words[0]), compare_first_characters);
printf("Sorted by first letter: \n");
for (int i = 0; i < n; i++) {
printf("%s ", words[i]);
}
printf("\n");
printf("Looking for a word that starts with '%s': ", key);
const void *found = bsearch(key, words, n, sizeof(words[0]), compare_first_characters);
printf("found '%s'\n", found ? *(const char **)found : "none");
return 0;
}
The both your comparison functions called with
bsearchare invalid.The function
bsearchis declared the following wayThat is
keymust be passed by reference through a pointer to it.However you are calling the function like
So in the first comparison function this double dereferencing
of the pointer that corresponds to the argument
keythat actually has the typeconst char *according to its declarationinvokes undefined behavior. You are wrong saying that using the first comparison function the program works correctly. Try for example to use another string as key as for example
"y".You have to write the function call like
Pay attention to that the function
strcmpcompares whole strings. So to use the second comparison function you need to specify a string literal that is equal to one of the strings of the array In this case the both comparsion functions will work correctly.And relative to the first comparison function then you should cast the void pointers to the unsigned character type
const unsigned char **.Here is your updated program.
The program output is
By the way as all strings in the array differ by the first character then you can sort the array using the first comparison function and then search a target string as for example
"yellow"or"green"using the second comparison function in the call ofbsearch.Here is updated previous program.
Its output is