|
|
NEW INTERVIEW QUESTIONS.COM
LANGUAGES INTERVIEW QUESTIONS
C INTERVIEW QUESTIONS DETAILS
NewInterviewQuestions.com - Home for World's Largest Interview Questions Website.
A D V E R T I S E M E N T
Question |
When would you use a pointer to a function? |
Answer
|
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. 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.
© NewInterviewQuestions.com
|
If you have the better answer, than send it to us. We will display your answer after the approval.
Rate the above answer. Help us to know about the answer.
Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.
|
|
|