English
Language : 

CC78K0S Datasheet, PDF (229/520 Pages) NEC – C Compiler Ver.1.30 or Later Language
CHAPTER 10 LIBRARY FUNCTIONS
5-15 bsearch (normal model only)
Utility Functions
FUNCTION
The bsearch function performs a binary search.
HEADER
stdlib.h
FUNCTION PROTOTYPE
void *bsearch(const void *key,const void *base,size_t nmemb,
size_t size,int (*compare)(const void *,const void *));
Function
bsearch
Arguments
Return Value
key ... Pointer to key for which
search is made
base ... Pointer to sorted array
which contains information to
search
nmemb ... Number of array
elements
size ... Size of an array
compare ... Pointer to function
used to compare two keys
• Pointer to the first member
that matches “key” if the
array contains the key;
• Null pointer if the key is not
contained in the array
EXPLANATION
• The bsearch function performs a binary search on the sorted array pointed to by base and returns a pointer
to the first member that matches the key pointed to by key. The array pointed to by base must be an array
which consists of nmemb number of members each of which has the size specified by size and must have
been sorted in ascending order.
• The function pointed to by compare takes two arguments (key as the 1st argument and array element as the
2nd argument), compares the two arguments, and returns:
- Negative value if the 1st argument is less than the 2nd argument
- 0 if both arguments are equal
- Positive integer if the 1st argument is greater than the 2nd argument
• When the -ZR option is specified, the function passed to the argument of the bsearch function must be a
pascal function.
User’s Manual U14872EJ1V0UM
229