User Tools

Site Tools


c:stdlib.h:bsearch

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

c:stdlib.h:bsearch [2024/02/16 01:04] (current)
Line 1: Line 1:
 +{{keywords>wiki library source code example reference}}
 +===== bsearch =====
 +
 +<code c>
 +    #include <stdlib.h>
 +    void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compare)(const void *, const void *));
 +</code>
 +
 +
 +**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 =====
 +<code c>
 +#include <stdio.h> /* including standard library */
 +//#include <windows.h> /* uncomment this for Windows */
 +
 +#include <stdlib.h>
 +#include <string.h>
 +
 +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;
 +}
 +
 +</code>
 +
 +==== output of bsearch(): ====
 +    user@host:~$ ./bsearch 
 +    Found "bsearch" in the my_strings array
 +
 +    
  

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
c/stdlib.h/bsearch.txt · Last modified: 2024/02/16 01:04 (external edit)

Impressum Datenschutz