/ ************************************************************************
*
* Purpose: To demonstrate the 'bsearch' function.
*
* Author: M.J. Leslie
*
* Date: 04-Jun-95
*
************************************************************************/
#include <stdlib.h>
#define NUM 11
static int func (void *, void *);
/************************************************************************/
main()
{
/* List of numbers. */
int numbers[NUM]={3,4,7,9,11,13,15,17,19,21,23};
int key=22; /* number to find. */
int * result; /* Result of 'bsearch' */
/* Search for 'key' in 'numbers'*/
result = bsearch (&key, numbers, NUM, sizeof(numbers[0]), (void *)func);
printf("%d ", key);
(result) ? puts("found") : puts("not found");
}
/************************************************************************/
static int func (void *a, void *b)
{
printf("%d %d \n", *(int *)a, *(int *)b); /* Diagnistics. */
/* Compare the two numbers */
if (*(int *)a == *(int *)b) return(0);
if (*(int *)a < *(int *)b) return(-1);
return (1);
}
file: /Techref/language/ccpp/cref/EXAMPLES/bsearch.c, 1KB, , updated: 1997/4/21 09:52, local time: 2024/9/7 08:33,
|
| ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://linistepper.com/techref/language/ccpp/cref/EXAMPLES/bsearch.c"> language ccpp cref EXAMPLES bsearch</A> |
Did you find what you needed?
|