Back to index

glibc  2.9
Functions
bsearch.c File Reference
#include <stdlib.h>

Go to the source code of this file.

Functions

void * bsearch (const void *key, const void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *))

Function Documentation

void* bsearch ( const void *  key,
const void *  base,
size_t  nmemb,
size_t  size,
int(*)(const void *, const void *)  compar 
)

Definition at line 25 of file bsearch.c.

{
  size_t l, u, idx;
  const void *p;
  int comparison;

  l = 0;
  u = nmemb;
  while (l < u)
    {
      idx = (l + u) / 2;
      p = (void *) (((const char *) base) + (idx * size));
      comparison = (*compar) (key, p);
      if (comparison < 0)
       u = idx;
      else if (comparison > 0)
       l = idx + 1;
      else
       return (void *) p;
    }

  return NULL;
}

Here is the caller graph for this function: