Concept
How does lsearch() search an array and insert a missing element?
ComputerScienceOne / Searching
"In the same library, there is another linear search function:\n\nvoid *lsearch(const void *key, void *base, size_t *nmemb, size_t size, int(*compar)(const void *, const void *));\n\nIt differs in that if it does not find a matching element, it still returns NULL but also attempts to insert the element being searched for at the end of the array. The function will assume there is enough room at the end of the array to accommodate the inserted element (if not, the behavior is undefined). This behavior is hinted at by the fact that base is not const. Moreover, nmemb is passed by reference. If the function inserts the element, it will also increment nmemb variable to reflect this new element. It is your responsibility to ensure that there is enough valid memory to accommodate any inserts when using lsearch()."
Related Ideas
- How are qsort(), lfind(), and bsearch() used in C examples?ComputerScienceOne · Examples
- What is the general searching problem?ComputerScienceOne · Searching
- How can comparator functions order Student structures?ComputerScienceOne · Comparator Functions
- What is a comparator function and how does it determine element order?ComputerScienceOne · Comparator Functions
- Why are qsort(), lfind(), and bsearch() useful for generic programming?ComputerScienceOne · Examples
- How does the C standard library’s qsort() function work?ComputerScienceOne · Sorting
- How does binary search work with arrays and lists?ComputerScienceOne · Sorting
- How do Java collections perform a linear search?ComputerScienceOne · Searching