2 .\" Initial implementation:
3 .\" Copyright (c) 2002 Robert Drehmel
4 .\" All rights reserved.
6 .\" As long as the above copyright statement and this notice remain
7 .\" unchanged, you can do what ever you want with this file.
15 .Nd linear search and append
22 .Fa "const void *key" "void *base" "size_t *nelp" "size_t width"
23 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
27 .Fa "const void *key" "const void *base" "size_t *nelp" "size_t width"
28 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
35 functions walk linearly through an array and compare each element with
36 the one to be sought using a supplied comparison function.
41 points to an element that matches the one that is searched.
42 The array's address in memory is denoted by the
45 The width of one element (i.e., the size as returned by
50 The number of valid elements contained in the array (not the number of
51 elements the array has space reserved for) is given in the integer pointed
56 argument points to a function which compares its two arguments and returns
57 zero if they are matching, and non-zero otherwise.
59 If no matching element was found in the array,
63 into the position after the last element and increments the
72 return a pointer to the first element found.
73 If no element was found,
75 returns a pointer to the newly added element, whereas
89 element_compare(const void *p1, const void *p2)
91 int left = *(const int *)p1;
92 int right = *(const int *)p2;
94 return (left - right);
98 main(int argc, char **argv)
100 const int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
101 size_t element_size = sizeof(array[0]);
102 size_t array_size = sizeof(array) / element_size;
106 printf("Enter a number: ");
107 if (scanf("%d", &key) != 1) {
108 printf("Bad input\en");
109 return (EXIT_FAILURE);
112 element = lfind(&key, array, &array_size, element_size,
116 printf("Element found: %d\en", *(int *)element);
118 printf("Element not found\en");
120 return (EXIT_SUCCESS);
139 functions appeared in
143 they reappeared conforming to