www.digitalmars.com Home | Search | CTG | RTL | IDDE | STL | C/C++ Compiler | D
Last update Thu Apr 13 20:39:46 2006
Runtime Library
Reference


· Constants
· Data types

Standard C
· assert.h
· complex.h
· ctype.h
· fenv.h
· float.h
· locale.h
· math.h
· setjmp.h
· signal.h
· stdarg.h
· stddef.h
· stdio.h
· stdlib.h
· string.h
· time.h


Standard C++
· IOstream
· new


Win32
· gc.h


DOS, DOS32, Win16
· bios.h
· cerror.h
· disp.h
· dos.h
· dos.h part 2
· emm.h
· handle.h
· int.h
· msmouse.h
· sound.h
· swap.h
· tsr.h
· winio.h


Other C
· bitops.h
· conio.h
· controlc.h
· direct.h
· fltpnt.h
· io.h
· page.h
· process.h
· search.h
· sys\stat.h
· tabsize.h
· trace.h
· utime.h
· unmangle.h
· util.h


Other C++
· regexp.h
· class complex


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