Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!columbia!sylvester.columbia.edu!t-lee
From: t-lee@sylvester.columbia.edu (Tim Lee)
Newsgroups: comp.lang.c
Subject: bsearch implementation help
Message-ID: <4220@columbia.UUCP>
Date: Wed, 14-Jan-87 19:32:04 EST
Article-I.D.: columbia.4220
Posted: Wed Jan 14 19:32:04 1987
Date-Received: Thu, 15-Jan-87 04:08:23 EST
Sender: nobody@columbia.UUCP
Reply-To: t-lee@sylvester.columbia.edu (Tim Lee)
Organization: Columbia University CS Department
Lines: 55


I'm attempting to write the equivalent of the bsearch function call
that is available in UNIX SYSV.  The problem that I am having is that
I don't know how to pass two array elements to the compare function
that the user supplies.  I can determine the address of any element in
the array, but do not know how to use the key as an array element...

The specifications for this routine should read exactly like the MAN
page for the bsearch function in UNIX.  Does anybody out there have a
clue as to how I should go about doing this?

Please try to send replies to: 

(internet)  x1.micky@cu20b.columbia.edu
  (usenet)  ...seismo!columbia!cu20b!x1.micky
                              !kirkwood!m-liu



Here is the unfinished routine:
----------------------------------------------------------------------

#include 


char *bsearch(key,base,num,width,compare)

char *key;
char *base;
unsigned num;
unsigned width;
int  (*compare)();


{
int  cond;
char *mid;

int  llo = 0;
int  hhi = num - 1;
int  mmi;

     while (llo <= hhi)
     {
          mmi = (llo + hhi) /2;
          mid = base + (width * mmi);
          if ((cond = (*compare)(key,mid)) < 0)
               hhi = mmi - 1;
          else if (cond > 0)
               llo = mmi + 1;
          else
               return(mid);
     }
     return((char *)NULL);
}