This is a help page from Borland C++ 3.1 You may need to change to font to
a mono font in other to read this.
bsearch, lfind,
lsearch, and qsort <STDLIB.H, SEARCH.H>
bsearch performs a binary search
lfind and lsearch perform a linear search
qsort sorts using the quicksort algorithm
Declaration:
void *bsearch(const void *key, const void *base, size_t nelem,size_t width,
int (*fcmp)(const void*, const void*));
void *lfind(const void *key, const void *base, size_t *num,size_t width, int
(*fcmp)(const void *, const void*));
void *lsearch(const void *key, void *base, size_t *num,size_t width, int
(*fcmp)(const void *, const void *));
void qsort(void *base, size_t nelem,size_t width, int (*fcmp)(const void *,
const void *));
Remarks:
Function What It Does
bsearch Makes a binary search for the value *key in a table (array)
of nelem elements in memory
lfind Makes a linear search for *key in an array of sequential
records
lsearch Makes a linear search for *key in a table. If *key is not in
the table, lsearch appends it (the search key) to the table.
qsort Is an implementation of the "median of three" variant of the
quicksort algorithm
Argument What It Is/Points To
base The base (0th element) of the search table
fcmp A user-defined comparison routine that compares two items
and returns a value based on the comparison
key The item to be searched for (the search key)
nelem The number of entries in the table
num The number of entries in the table
width The number of bytes in each entry
NOTE:
# Because lsearch performs a linear search, the table entries do not need
to be sorted before the function call.
# Because bsearch performs a binary search, the first matching entry is
not necessarily the first entry in the table.
Return Value:
Function On Failure On Success
bsearch 0 (No match) The address of the first
lfind NULL (No match) entry in the table that
lsearch matches the search key
qsort None None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
bsearch | Yes | Yes | Yes | Yes | |
lfind | Yes | Yes | Yes | | |
lsearch | Yes | Yes | Yes | | |
qsort | Yes | Yes | Yes | Yes | |
+-----+------+---------+--------+----------+
Examples:
bsearch example lfind example lsearch example qsort example
lsearch example
/* lsearch example */
#include <stdlib.h>
#include <stdio.h>
#include <string.h> /* for strcmp declaration */
/* initialize number of colours */
char *colours[10] = { "Red", "Blue", "Green" };
int ncolours = 3;
int colourscmp(char **arg1, char **arg2)
{
return(strcmp(*arg1, *arg2));
}
int addelem(char *key)
{
int oldn = ncolours;
lsearch(key, colours, (size_t *)&ncolours, sizeof(char *),
(int(*)(const void *,const void *))colourscmp);
return(ncolours == oldn);
}
int main(void)
{
int i;
char *key = "Purple";
if (addelem(key))
printf("%s already in colours table\n", key);
else
{
strcpy(colours[ncolours-1],key);
printf("%s added to colours table\n", key);
}
printf("The colours:\n");
for (i = 0; i < ncolours; i++)
printf("%s\n", colours
);
return 0;
}
--
From _Christopher (M2M).
RefCode:44CXcxTCdff4 V04
void DeadEnds() {for(;;} //
If replying by email please included ##71; on the subject line followed by
your subject,
any post without the ##71; tag WILL be deleted. I.e. "##71; Thank for the
help."