bsearch() — Search Arrays

Format

#include <stdlib.h>
void *bsearch(const void *key, const void *base,
              size_t num, size_t size,
              int (*compare)(const void *key, const void *element));

Language Level: ANSI

Threadsafe: Yes.

Description

The bsearch() function performs a binary search of an array of num elements, each of size bytes. The array must be sorted in ascending order by the function pointed to by compare. The base is a pointer to the base of the array to search, and key is the value being sought.

The compare argument is a pointer to a function you must supply that compares two items and returns a value specifying their relationship. The first item in the argument list of the compare() function is the pointer to the value of the item that is being searched for. The second item in the argument list of the compare() function is a pointer to the array element being compared with the key. The compare() function must compare the key value with the array element and then return one of the following values:

Value Meaning
Less than 0 key less than element
0 key identical to element
Greater than 0 key greater than element

Return Value

The bsearch() function returns a pointer to key in the array to which base points. If two keys are equal, the element that key will point to is unspecified. If the bsearch() function cannot find the key, it returns NULL.

Example that uses bsearch()

This example performs a binary search on the argv array of pointers to the program parameters and finds the position of the argument PATH. It first removes the program name from argv, and then sorts the array alphabetically before calling bsearch(). The compare1() and compare2() functions compare the values pointed to by arg1 and arg2 and return the result to the bsearch() function.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
int compare1(const void *, const void *);
int compare2(const void *, const void *);
 
main(int argc, char *argv[])
{                        /* This program performs a binary        */
  char **result;         /* search on the argv array of pointers  */
  char *key = "PATH";    /* to the program parameters.  It first  */
  int i;                 /* removes the program name from argv    */
                         /* then sorts the array alphabetically   */
  argv++;                /* before calling bsearch.               */
  argc--;
 
  qsort((char *)argv, argc, sizeof(char *), compare1);
 
  result = (char**)bsearch(&key, (char *)argv, argc, sizeof(char *), compare2);
  if (result != NULL)  {
      printf("result =<%s>\n",*result);
   }
  else printf("result is null\n");
}
 
         /*This function compares the values pointed to by arg1  */
         /*and arg2 and returns the result to qsort.  arg1 and   */
         /*arg2 are both pointers to elements of the argv array. */
 
int compare1(const void *arg1, const void *arg2)
{
    return (strcmp(*(char **)arg1, *(char **)arg2));
}
 
         /*This function compares the values pointed to by arg1  */
         /*and arg2 and returns the result to bsearch            */
         /*arg1 is a pointer to the key value, arg2 points to    */
         /*the element of argv that is being compared to the key */
         /*value.                                                */
 
int compare2(const void *arg1, const void *arg2)
{
    return (strcmp(*(char **)arg1, *(char **)arg2));
}
/********************  Output should be similar to:  *************
 
result = <PATH>
 
****************** When the input on the i5/OS command line is ********
 
CALL BSEARCH PARM(WHERE IS PATH IN THIS PHRASE'?')
 
*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]