When would you use a pointer to a function?

Pointers to functions are interesting when you pass them to other functions. A function that takes function pointers says, in effect, “Part of what I do can be customized. Give me a pointer to a function, and I’ll call it when that part of the job needs to be done. That function can do its part for me.” This is known as a “callback.” It’s used a lot in graphical user interface libraries, in which the style of a display is built into the library but the contents of the display are part of the application. As a simpler example, say you have an array of character pointers (char*s), and you want to sort it by the value of the strings the character pointers point to. The standard qsort() function uses function pointers to perform that task. (For more on sorting, see Chapter III, “Sorting and Searching Data.”) qsort() takes four arguments, Ø      a pointer to the beginning of the array, Ø      the number of elements in the array, Ø      the size of each array element, and Ø      a comparison function, and returns an int.

Showing Answers 1 - 1 of 1 Answers

abhimanipal

  • Jan 30th, 2010
 

Another application could be when the function is not located in the code segment. Many time the size of the Source code is greater than 64 KB.  In such a scenario if we want to call a function, we have to use a far pointer to call that function. Not sure though

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions