_lsearch

Performs a linear search of an array, and adds an element to the end of the list if the element is not found.

void * _lsearch (void * key, void * base, size_t * num, size_t width, int (*compare)(void * element1, void * element2));

void * lsearch (void * key, void * base, size_t * num, size_t width, int (*compare)(void * element1, void * element2));

Required Header
<stdlib.h>

Return Value

Each of these functions returns a pointer to the found key-element, or a pointer to the newly added element if a match could not be found.

Parameters

key

  Key representing the object to find

base

  Base pointer of the array to search

num

  A pointer to the number of elements in the array

width

  The width in bytes of each element

compare

  A user written compare function to that takes two parameters:

element1, element2

  pointer to the key, and an element within the array to be checked

Remarks

The _lsearch function calls the user supplied function compare using the key and elements within the array until the desired key-element is found or the search is exhausted. The compare function should return the following values:

  < 0 : element1 is less than element2
  = 0 : element1 is equal to element2
  > 0 : element1 is greater than element2

Standard Library

See Also    bsearch, _lfind, qsort