词条 | bsearch |
释义 | § 函数名 bsearch § 功能 二分法搜索 § 语法 void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *)); § 程序演示 #include <stdlib.h> #include <stdio.h> #define NELEMS(arr) (sizeof(arr) / sizeof(arr【0】)) int numarray【】 = {123, 145, 512, 627, 800, 933}; int numeric (const int *p1, const int *p2) { return(*p1 - *p2); } int lookup(int key) { int *itemptr; /* The cast of (int(*)(const void *,const void*)) is needed to avoid a type mismatch error at compiletime*/ itemptr = bsearch (&key, numarray, NELEMS(numarray), sizeof(int), (int(*)(const void *,const void *))numeric); return (itemptr != NULL); } int main(void) { if (lookup(512)) printf("512 is in the table.\"); else printf("512 isn't in the table.\"); return 0; } |
随便看 |
百科全书收录594082条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。