LSEARCH - linear search and update table.
(Compatible with UNIX System V C)
Usage:
#include <stdio.h>
#include <search.h>
char *lsearch();
ptr = lsearch( key, table, tsize_p, keysize, compar);
Where:
- char *key;
- points to the key you are searching for. This can
actually be any type of information, but the pointer must
be cast into (char *). If this key is not found in the
table, it will be added to the end of the table.
- char *table;
- points to the beginning of a table that contains
information to search. This need not be sorted in any
order. It should have enough space to hold a new key, in
the event that the desired key is not found in the
existing table.
- unsigned int *tsize_p;
- points to an unsigned integer that gives the number of
elements in the table. If the desired key cannot be found
in the table and is therefore added to the end of the
table, the unsigned integer will be incremented to
reflect that the table now has one more element.
- int keysize;
- is equal to "sizeof(*key)", i.e. the size (in
bytes) of the key you are searching for.
- int (*compar)(char *,char *);
- is a pointer to a user-defined function that determines
whether or not two keys are equal. This function should
take two (char *) arguments; one will point to the key
you are looking for and the other will point to a key in
the table. The function should return a zero if the two
keys are equal, and a non-zero value otherwise.
- char *ptr;
- points to a table element that matches "key".
If no match was found in the original table, this will
point to the new key where it was added at the end of the
table.
Description:
"lsearch" performs a linear search through the
elements of a table. It uses the user-supplied "compar"
function to compare elements in the table. If the desired key is
not found in the table, the search key is added to the end of the
table.
Note that "compar" does not have to do a
byte-by-byte comparison between the key and the table elements.
For example, the table may be an array of C structures and
"compar" may only look at a particular field when
comparing two structures. In this case, the original search key
might just be a dummy, with only the significant field filled in.
See Also:
- expl c lib lfind
- -- linear search without update
- expl c lib bsearch
- -- binary search
- expl c lib hsearch
- -- hash functions
- expl c lib tsearch
- -- tree search
Copyright © 1996, Thinkage Ltd.