{{keywords>wiki library source code example reference}}
===== bsearch =====
#include
void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compare)(const void *, const void *));
**key** - The element that is searched for\\
**base** - pointer to the first element of the array to be searched\\
**nitems** - number of elements in the array\\
**size** - size of a single element\\
**compare** - comparison function to compare two elements together.\\
== return values: ==
pointer to an entry in the array that matches the search key.\\
If key not found, NULL pointer returned.\\
===== bsearch c Sourcecode Example =====
#include /* including standard library */
//#include /* uncomment this for Windows */
#include
#include
int main ( void )
{
char my_key[10] = "bsearch";
int my_elementCount = 4;
int my_size = 20;
char my_strings[][30] = {"c", "example", "bsearch", "coderefer" , "someusefulinformation"};
/* strcmp is the compare function for this bsearch */
char *return_value = (char*) bsearch(my_key, my_strings, my_elementCount, my_size, (int(*)(const void*, const void*))strcmp);
if (return_value) {
printf("Found \"%s\" in the my_strings array.\n", return_value);
}
else {
printf("Did not found \"%s\" in the my_strings array.\n", my_key);
}
return 0;
}
==== output of bsearch(): ====
user@host:~$ ./bsearch
Found "bsearch" in the my_strings array