aboutsummaryrefslogtreecommitdiff
path: root/6.1.1/key.c
diff options
context:
space:
mode:
authorsinanmohd <sinan@firemail.cc>2023-05-01 22:12:45 +0530
committersinanmohd <sinan@firemail.cc>2023-05-01 22:13:41 +0530
commit16c2c412f3062924ad7869068730b2c56afbcff8 (patch)
treead1dbf51430bf72064503ae0122f435b13a9fc6a /6.1.1/key.c
parentc5b2cb7bfd3a59036cc3ad7088f4ca3765025f58 (diff)
6.1.1: 6.1 but with pointers instead of array indices
Diffstat (limited to '6.1.1/key.c')
-rw-r--r--6.1.1/key.c23
1 files changed, 23 insertions, 0 deletions
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 <string.h>
+#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;
+}