所以我试图使用 C 中的 bsearch 函数在字符串数组中进行二进制搜索。出于某种原因,它总是返回 NULL。对于 bsearch 的比较功能,我没有使用 strcmp,我自己做了一个,令人惊讶的是,它适用于我手动实现二进制搜索功能。无论如何,我会稍微解释一下,这是我的主要代码:
int main(){
int n;
scanf("%d", &n);
if(n<=0) qerror();
char ** words = (char **)calloc(n, sizeof(char *));
if(words == NULL) qerror();
int i = 0;
for(;i<n;i++){
words[i] = (char *)calloc(MAX, sizeof(char));
if(words[i] == NULL) qerror();
scanf("%s", words[i]);
}
char seek_word[MAX];
scanf("%s", seek_word);
char ** c = (char **)bsearch(seek_word, words, n, sizeof(char *), &b_compare_words);
if(c == NULL) printf("-1\n");
else printf("%d\n", c - words);
//printf("%d\n", bin_search_string(words, n, seek_word)); //manuelna binarna pretraga
free(words);
return 0;
}
这段代码的第一部分只是创建一个字符串数组(命名词),其中包含 N 个长度为 MAX 的字符串(这是在开头定义的宏)。我已经测试了这部分并且它可以工作,所以这不是问题。
之后,输入 seek_word,这正是我们试图在 'words' 中找到的单词。我调用 bsearch 作为比较函数,我使用了我自己创建的函数。这是它的样子:
int compare_words(char * a, char * b){
int i = 0;
while(a[i] && b[i]){
char ac = tolower(a[i]), bc = tolower(b[i]);
if(ac < bc) return -1;
else if (ac > bc) return 1;
i++;
}
return 0;
}
int b_compare_words(const void * a, const void * b){
return compare_words((char *)a, (char *)b);
}
实际的比较工作是在 compare_words 函数中完成的,我从 b_compare_words 函数中调用了该函数,我刚刚将 void 指针转换为 char 指针。无论我对此进行什么测试,我都会得到 -1(意味着 bsearch 返回的指针为 NULL)。现在这是有趣的部分。除此之外,我制作了自己的二进制搜索函数,它做同样的事情并且它可以工作(你可以看到我在下面注释掉的部分)。该函数称为 bin_search_string,它看起来像这样:
int bin_search_string(char ** a, int n, char * x){
int l_index = 0;
int r_index = n-1;
int check_index;
while(l_index <= r_index){
check_index = l_index + (r_index - l_index)/2;
int test = compare_words(x, a[check_index]);
if(test == 0) return check_index;
else if(test < 0) l_index = check_index + 1;
else r_index = check_index - 1;
}
return -1;
}
如您所见,该函数使用了我也传递给 bsearch 的相同 compare_words 函数。但是当我使用自己的函数测试我的程序时,它确实有效(返回找到字符串的索引)。那么,有什么想法为什么 bsearch 不起作用?提前致谢。