bsearch.c 339 B

123456789101112131415
  1. #include <stdlib.h>
  2. void *bsearch(const void *key, const void *base, size_t nmemb, size_t size,
  3. int (*compar)(const void *, const void *)) {
  4. /* Stupid naive implementation */
  5. const char * b = base;
  6. size_t i = 0;
  7. while (i < nmemb) {
  8. const void * a = b;
  9. if (!compar(a,key)) return (void *)a;
  10. i++;
  11. b += size;
  12. }
  13. return NULL;
  14. }