|
Lines 81-86
Link Here
|
| 81 |
Both functions return |
81 |
Both functions return |
| 82 |
.Dv NULL |
82 |
.Dv NULL |
| 83 |
if an error occurs. |
83 |
if an error occurs. |
|
|
84 |
.Sh EXAMPLES |
| 85 |
.Bd -literal |
| 86 |
|
| 87 |
#include <search.h> |
| 88 |
#include <stdio.h> |
| 89 |
#include <stdlib.h> |
| 90 |
|
| 91 |
static int |
| 92 |
element_compare(const void *p1, const void *p2) |
| 93 |
{ |
| 94 |
int left = *(const int *)p1; |
| 95 |
int right = *(const int *)p2; |
| 96 |
|
| 97 |
return (left - right); |
| 98 |
} |
| 99 |
|
| 100 |
int |
| 101 |
main(int argc, char **argv) |
| 102 |
{ |
| 103 |
const int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 104 |
size_t element_size = sizeof(array[0]); |
| 105 |
size_t array_size = sizeof(array) / element_size; |
| 106 |
int key; |
| 107 |
void *element; |
| 108 |
|
| 109 |
printf("Enter a number: "); |
| 110 |
if (scanf("%d", &key) != 1) { |
| 111 |
printf("Bad input\n"); |
| 112 |
return (EXIT_FAILURE); |
| 113 |
} |
| 114 |
|
| 115 |
element = lfind (&key, array, &array_size, element_size, element_compare); |
| 116 |
|
| 117 |
if (element != NULL) |
| 118 |
printf("Element found: %d\n", *(int *)element); |
| 119 |
else |
| 120 |
printf("Element not found\n"); |
| 121 |
|
| 122 |
return (EXIT_SUCCESS); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
.Ed |
| 84 |
.Sh SEE ALSO |
127 |
.Sh SEE ALSO |
| 85 |
.Xr bsearch 3 , |
128 |
.Xr bsearch 3 , |
| 86 |
.Xr hsearch 3 , |
129 |
.Xr hsearch 3 , |