digitalmars.com                      
Last update Wed Mar 30 15:57:57 2016

search.h


_lfind

Header
search.h
Prototype
void *_lfind(const void *key, const void *base, unsigned int *num, unsigned int width, int(__cdecl *compare) (const void *, const void *));
Description
The _lfind function performs a linear search for the value key in the array pointed to by base. The array has num elements; each element has a size of width bytes.

The compare argument is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. A non-zero value indicates that the elements are different; 0 indicates that the elements are the same.

Synonym
Function: lfind
Return Value
A pointer to the first array element that matches key. If no match is found, NULL is returned.
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
bsearch
_lsearch
Example
/* Example of _lfind */

#include <stdio.h>
#include <search.h>

int compare(int *x, int *y)
{
 return *x -*y;
}

void main()
{
 int array[5] = {44, 69, 3, 17, 23};
 size_t elems = 5;
 int key = 69;
 int *result;

 result = (int *)_lfind(&key, &array,
     &elems,
     sizeof(int),
     (int(*)(const void *, const void *))compare);
 if (result)
  printf("Key %d found in linear search\n",
         key);
 else
  printf("Key %d not found in linear search\n",
         key);
}
Output
Key 69 found in linear search

_lsearch

Header
search.h
Prototype
void *_lsearch(const void *key, const void *base, unsigned int *num, unsigned int width, int (__cdecl *compare)(const void *, const void *));
Description
The _lsearch functions performs a linear search for the value key in the array pointed to by base. The array has num elements; each element has a size of width bytes. If the key value is not found during the search, the lsearch function adds it to the end of the array, and *num is incremented by 1.

The compare argument is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. A non-zero value indicates that the elements are different; 0 indicates that the elements are the same.

Synonym
Function: lsearch
Return Value
A pointer to the first array element that matches key. If no match is found, the function returns a pointer to the newly added item at the end of the array.
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
bsearch
_lfind
Example
/* Example of _lsearch */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <search.h>

char *animals[10] =
{
   "Horse",
   "Dog",
   "Cat",
   "Goat",
   "Peacock"
};

size_t elems = 5;

int compare(char **x, char **y)
{
 return strcmp(*x, *y);
}

int addelem(char *key)
{
 size_t num = elems;

 _lsearch(&key, animals, &num,
    sizeof(char *),
    (int (*)(const void *, const void *))compare);
 return (elems == num);
}

void main()
{
 char *key = "Donkey";

 if (addelem(key))
    printf("Animal \"%s\" already exists in array\n",
           key);
 else
    printf("\"%s\" added to animals array\n",
           key);
}
Output
"Donkey" added to animals array
Home | Compiler & Tools | IDDE Reference | STL | Search | Download | Forums