0

Is there a good reason to pass pointer of function to another function in C. I do not see a general use case of pointer of function.

If someone can give some use case where pointer of function is the only way to achieve some specific use case that would be great.

Ankit Zalani
  • 3,068
  • 5
  • 27
  • 47
  • 2
    Why not check out the answer here: http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work – Dr. Cogent Jun 23 '15 at 18:18
  • 1
    If you want to write a generic sort algorithm then passing a pointer to a function that sorts your particular type is a great idea. Otherwise you need to write a sort algorithm for every different type. Try typing `man qsort` into a shell to see in more detail. Also any function that needs to callback later will probably take a function pointer. – James Snook Jun 23 '15 at 18:18

3 Answers3

5

One use case is to have a async callback function.

You pass a pointer of a callback() function to a worker() function.

worker(void* work, void *callback)

Once worker function has completed the work, it will call the callback() function.

This is essentially a subscriber publisher pattern in Object Oriented languages (C++/Java).

Harman
  • 1,571
  • 1
  • 19
  • 31
  • 1
    As others have pointed, a callback is not the only use case for it.In addition to it, uou can pass a comparator function (like in qsort() example), or create a jump table. – Vitor Jun 23 '15 at 18:28
  • @Vitor: I read "one use case" which is certainly correct. Nowhere this answer claims it is "the only use case". – undur_gongor Jun 23 '15 at 18:32
  • @Vitor yes, you are right. callback is not the only use case. And that is what I have written in my answer. – Harman Jun 23 '15 at 18:34
  • @Harman: that's right, I've misread your answer somehow, sorry. I've changed my down vote to a upvote – Vitor Jun 23 '15 at 18:39
3

A general use where pointer to a function is passed as an argument to a function is in qsort function.

  void qsort(void *base, size_t nmemb, size_t size,
              int (*compar)(const void *, const void *));  

Another use:
Suppose you wanted a program to plot equations. You would give it a range of x and y values (perhaps -10 to 10 along each axis), and ask it to plot y = f(x) over that range (e.g. for -10 <= x <= 10).
How would you tell it what function to plot? By a function pointer, of course!
The plot function could then step its x variable over the range you specified, calling the function pointed to by your pointer each time it needed to compute y. You might call

plot(-10, 10, -10, 10, sin)

to plot a sine wave, and

plot(-10, 10, -10, 10, sqrt)

to plot the square root function.

But in general, better to avoid using function pointers unless and until it is necessary.

haccks
  • 104,019
  • 25
  • 176
  • 264
3

Function pointers as arguments serve the same general purpose as other kinds of pointers: indirection. They are different in that it is behavior that is indirected instead of data, but many of the same considerations apply. By designing my function to accept a pointer to another function, I can allow the caller to decide aspects of my function's behavior (because my function will call the pointed-to function as part of its work).

The canonical use case is probably the standard library's qsort() function, which can sort data of any type by virtue of accepting a pointer to the function to use to compare values. If it could not accept a function pointer, then you would need a different qsort() implementation for every data type you wanted to sort.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157