
bsearch
7-26
Array Search
bsearch
Syntax
#include <stdlib.h>
void *
bsearch
(const void *key, const void *base, size_t nmemb,
size_t size, int (*compar)(const void *, const void *));
Defined in
bsearch.c in rts.src
Description
The bsearch function searches through an array of nmemb objects for a mem-
ber that matches the object that key points to. Argument base points to the first
member in the array; size specifies the size (in bytes) of each member.
The contents of the array must be in ascending order. If a match is found, the
function returns a pointer to the matching member of the array; if no match is
found, the function returns a null pointer (0).
Argument compar points to a function that compares key to the array ele-
ments. The comparison function should be declared as:
int cmp(const void *ptr1, const void *ptr2);
The cmp function compares the objects that prt1 and ptr2 point to and returns
one of the following values:
< 0
if *ptr1 is less than *ptr2
0
if *ptr1 is equal to *ptr2
> 0
if *ptr1 is greater than *ptr2
Example
#include <stdlib.h>
#include <stdio.h>
int list [ ] = {1, 3, 4, 6, 8, 9};
int diff (const void *, const void *0;
main( )
{
int key = 8;
int p =
bsearch
(&key, list, 6, 1, idiff);
/* p points to list[4] */
}
int idiff (const void *il, const void *i2)
{
return *(int *) i1 − *(int *) i2;
}
Содержание TMS320C2x
Страница 8: ...viii...
Страница 69: ...2 47 C Compiler Description...
Страница 159: ...6 36...
Страница 226: ...8 6...
Страница 250: ...Index Index 14 Z z shell option 2 2 2 4 2 14 overriding with c option 4 5 overriding with n option 2 13...