二进制搜索Ç [英] binary search C

查看:152
本文介绍了二进制搜索Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是二进制搜索功能的签名:

Here's the signature of the binary search function:

void *bsearch(const void *key, const void *base, size_t nmemb, size_t size,
              int (*compar)(const void *, const void *));

我不明白为什么我们要发送一个void *作为数组指针,而不是无效**,因为据我知道,如果我正在寻找一个int数组里面一个int,我应该送一个int *作为数组,而不是诠释...
请问implementaion有太大的不同(或者是不可能实现的),如果函数的签名是这样的:

I don't understand why we're sending a void* as the array pointer, instead of void**, since as far as I know, if i'm looking for an int inside an int array, I should send an int* as the array, not int... Would the implementaion be much different (or perhaps impossible to implement) if the function's signature was like this:

void *bsearch(const void *key, const void** base, size_t nmemb, size_t size,
              int (*compar)(const void *, const void *));

先谢谢了。

推荐答案

bsearch()的功能是pretty灵活,因为你可以传递一个指针任何的类型的数组。更改参数常量无效**基地将意味着你只能使用 bsearch ()的指针数组。

The bsearch() function is pretty flexible, because you can pass a pointer to any kind of array. Changing the base parameter to const void **base would imply that you could only use bsearch() on arrays of pointers.

如果您正在搜索 INT 的数组,那么你当然应该传递一个无效* 这是启动阵列的地址。

If you are searching an array of int, then you should certainly pass a void * which is the start address of your array.

这篇关于二进制搜索Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆