Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
bsearch.c File Reference
#include <stddef.h>
#include <stdlib.h>
Include dependency graph for bsearch.c:

Go to the source code of this file.

Functions

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

Function Documentation

◆ bsearch()

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

Definition at line 51 of file bsearch.c.

58 {
59  const char* base = base0;
60  size_t lim;
61  int cmp;
62  const void* p;
63 
64  for(lim = nmemb; lim != 0; lim >>= 1)
65  {
66  p = base + (lim >> 1) * size;
67  cmp = (*compar)(key, p);
68  if(cmp == 0)
69  {
70  return ((void*)(uintptr_t)p);
71  }
72  if(cmp > 0)
73  { /* key > p: move right */
74  base = (const char*)p + size;
75  lim--;
76  } /* else move left */
77  }
78  return (NULL);
79 }
#define NULL
Definition: stddef.h:15
int cmp(Bigint *a, Bigint *b)
Definition: misc.c:570

References cmp(), and NULL.