![Texas Instruments TMS320C2x User Manual Download Page 185](http://html1.mh-extra.com/html/texas-instruments/tms320c2x/tms320c2x_user-manual_1097073185.webp)
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;
}
Summary of Contents for TMS320C2x
Page 8: ...viii...
Page 69: ...2 47 C Compiler Description...
Page 159: ...6 36...
Page 226: ...8 6...