Add example.

PR:		177025
Submitted by:	Fernando <fernando.apesteguia@gmail.com>
Reviewed by:	theraven
This commit is contained in:
Joel Dahl 2013-04-21 10:30:19 +00:00
parent 309bbfe8d2
commit 15f764dd19
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=249721

View File

@ -8,7 +8,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd October 11, 2002
.Dd April 21, 2013
.Dt LSEARCH 3
.Os
.Sh NAME
@ -81,6 +81,47 @@ returns
Both functions return
.Dv NULL
if an error occurs.
.Sh EXAMPLES
.Bd -literal
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
static int
element_compare(const void *p1, const void *p2)
{
int left = *(const int *)p1;
int right = *(const int *)p2;
return (left - right);
}
int
main(int argc, char **argv)
{
const int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
size_t element_size = sizeof(array[0]);
size_t array_size = sizeof(array) / element_size;
int key;
void *element;
printf("Enter a number: ");
if (scanf("%d", &key) != 1) {
printf("Bad input\n");
return (EXIT_FAILURE);
}
element = lfind(&key, array, &array_size, element_size,
element_compare);
if (element != NULL)
printf("Element found: %d\n", *(int *)element);
else
printf("Element not found\n");
return (EXIT_SUCCESS);
}
.Ed
.Sh SEE ALSO
.Xr bsearch 3 ,
.Xr hsearch 3 ,