From 16c2c412f3062924ad7869068730b2c56afbcff8 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Mon, 1 May 2023 22:12:45 +0530 Subject: 6.1.1: 6.1 but with pointers instead of array indices --- 6.1.1/key.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 6.1.1/key.c (limited to '6.1.1/key.c') diff --git a/6.1.1/key.c b/6.1.1/key.c new file mode 100644 index 0000000..f6384fe --- /dev/null +++ b/6.1.1/key.c @@ -0,0 +1,23 @@ +#include +#include "key.h" + +struct key * +bsearch(char *word, struct key tab[], int n) +{ + int cond; + struct key *low, *high, *mid; + + low = tab; + high = tab + n - 1; + while (low <= high) { + mid = low + (high-low) / 2; + if ((cond = strcmp(word, mid->word)) < 0) + high = mid - 1; + else if (cond > 0) + low = mid + 1; + else + return mid; + } + + return NULL; +} -- cgit v1.2.3