Concept
How do callbacks make C functions more generic?
ComputerScienceOne / Function Pointers
"The main use for function pointers is so that references to functions can be passed as parameters to other functions. The passed function is known as a callback. This gives us the ability to write a more abstract and generic function. For example, in GUI programming, we frequently need to associate a particular function with a particular event. Suppose we create a button; we need to be able to specify what happens when that button gets clicked. We do so by providing a function as a callback to a registration function that associates the “click” event with the provided function. Thus, whenever a user clicks the button, the callback is invoked (“called back”).\n\nLet’s illustrate some syntax usage with another example. Suppose we want to create a getMax() function. We could write one function for arrays of integers, another for arrays of doubles, another for Student arrays that gets the student with the maximum GPA, then another for the ID, then another for the name and so on. Or, we could program a generic getMax() function that could be used for any type by taking a comparator function as a callback. To illustrate, consider the following non-generic function for integers.\n\nint getMax(const int *arr, int n) {\n\nint i, maxIndex = 0;\n\nfor(i=1; i<n; i++) {\n\nif(arr[maxIndex} < arr[i] {\n\n//we've found something larger, update the maxIndex:\n\nmaxIndex = i;\n\n}\n\n}\n\nreturn maxIndex;\n\n}\n\nThis simple function iterates through the array, keeping track of the maximum value found “so far” and updating it when it finds something larger. Because we’ve specified that arr is an array if int values, we can use the less-than comparison operator (line 4).\n\nNow let’s make it more generic: rather than taking an array of int values, it will now take a generic void array. Further, we will pass a function pointer to this function that references a generic comparator function that can be used to replace the less-than comparison operator.\n\nint getMax(const void *arr, int n,\n\nint(*cmp)(const void *, const void *)) {\n\nint i, maxIndex = 0;\n\nfor(i=1; i<n; i++) {\n\n...\n\n}\n\nreturn maxIndex;\n\n}"
Related Ideas
- How are generic getMax() functions used with integers and structures?ComputerScienceOne · Function Pointers
- 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
- How do you implement a comparator for integers in C?ComputerScienceOne · Comparator Functions
- How does the C standard library function qsort() work?ComputerScienceOne · Examples
- Why are qsort(), lfind(), and bsearch() useful for generic programming?ComputerScienceOne · Examples
- How can function pointers point to and call different functions?ComputerScienceOne · Generalized Rounding
- How are qsort(), lfind(), and bsearch() used in C examples?ComputerScienceOne · Examples