Concept
Why are qsort(), lfind(), and bsearch() useful for generic programming?
"The advantages to using qsort() (as well as lfind() and bsearch()) should be clear. There is no need to write a new function that reimplements the same algorithm for every possible ordering of every possible user-defined structure. We need only to create a comparator function and pass it to qsort(). There is less code, and less chance of bugs. The qsort() function is well-designed, optimized, and most importantly well-tested and proven. These functions represent a sort of “weak” form of polymorphic behavior found in more modern object-oriented programming languages and other languages that support “generic programming.” Polymorphism is the characteristic that the same code can be executed on different types, greatly reducing the need for duplicate code."
Related Ideas
- What are the advantages of using qsort() and related generic functions?ComputerScienceOne · Sorting
- How does the C standard library’s qsort() function work?ComputerScienceOne · Sorting
- What is a comparator function and how does it determine element order?ComputerScienceOne · Comparator Functions
- How do callbacks make C functions more generic?ComputerScienceOne · Function Pointers
- What is a function pointer and how is it declared?ComputerScienceOne · Function Pointers
- How do you implement a comparator for integers in C?ComputerScienceOne · Comparator Functions
- How can comparator functions order Student structures?ComputerScienceOne · Comparator Functions
- How can qsort() sort an array of strings using pointers?ComputerScienceOne · Sorting Pointers to Elements