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.
17 .Nd linear search and append
24 .Fa "const void *key" "void *base" "size_t *nelp" "size_t width"
25 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
29 .Fa "const void *key" "const void *base" "size_t *nelp" "size_t width"
30 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
37 functions walk linearly through an array and compare each element with
38 the one to be sought using a supplied comparison function.
43 points to an element that matches the one that is searched.
44 The array's address in memory is denoted by the
47 The width of one element (i.e., the size as returned by
52 The number of valid elements contained in the array (not the number of
53 elements the array has space reserved for) is given in the integer pointed
58 argument points to a function which compares its two arguments and returns
59 zero if they are matching, and non-zero otherwise.
61 If no matching element was found in the array,
65 into the position after the last element and increments the
74 return a pointer to the first element found.
75 If no element was found,
77 returns a pointer to the newly added element, whereas
91 element_compare(const void *p1, const void *p2)
93 int left = *(const int *)p1;
94 int right = *(const int *)p2;
96 return (left - right);
100 main(int argc, char **argv)
102 const int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
103 size_t element_size = sizeof(array[0]);
104 size_t array_size = sizeof(array) / element_size;
108 printf("Enter a number: ");
109 if (scanf("%d", &key) != 1) {
110 printf("Bad input\en");
111 return (EXIT_FAILURE);
114 element = lfind(&key, array, &array_size, element_size,
118 printf("Element found: %d\en", *(int *)element);
120 printf("Element not found\en");
122 return (EXIT_SUCCESS);
141 functions appeared in
145 they reappeared conforming to