2

sometimes,why do we pass the address of a function as parameter in C? why don't we just use the function directly? i came across such a code while using ADT of BST

IRock
  • 127
  • 3
  • 11
  • 2
    As a callback, for instance: qsort() – wildplasser Jul 19 '13 at 12:41
  • 2
    What is "ADT of BST"? – Cody Gray - on strike Jul 19 '13 at 12:41
  • 1
    There are numerous reasons. If you can show a coding example, it could be explained more specifically why those ideas come into play in that case. – lurker Jul 19 '13 at 12:41
  • 1
    possible duplicate of [What is the point of function pointers?](http://stackoverflow.com/questions/2592137/what-is-the-point-of-function-pointers) – Cody Gray - on strike Jul 19 '13 at 12:41
  • 1
    possible duplicate of [What are function pointers used for, and how would I use them?](http://stackoverflow.com/questions/1758564/what-are-function-pointers-used-for-and-how-would-i-use-them) – devnull Jul 19 '13 at 12:44
  • 1
    As an example: see my answer here: http://stackoverflow.com/a/9634059/905902 – wildplasser Jul 19 '13 at 12:46
  • I asked a related question back in time and I got pretty reasonable answers with good example (luckily more than one) : here [Is pointers to function as struct member useful in c?](http://stackoverflow.com/questions/12971995/is-pointers-to-function-as-struct-member-useful-in-c) – Grijesh Chauhan Jul 19 '13 at 13:30

4 Answers4

1

So that you can pass different functions, in order to vary behaviour, like polymorphism, but without classes with virtual methods, or other tricks.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

Benefits of Function Pointers:

  • Function pointers provide a way of passing around instructions for how to do something
  • You can write flexible functions and libraries that allow the programmer to choose behavior by passing function pointers as arguments
  • This flexibility can also be achieved by using classes with virtual functions

(from cprogramming.com)

Community
  • 1
  • 1
t7bdh3hdhb
  • 438
  • 3
  • 15
0

The function that gets a pointer to another function as argument can call back the argument function to do some repeated operation. Hence the name callback function. The function gets more universal because it can do different thing when you switch the callback implementation. It is used for example in Windows GUI programming, when you register a call back function that will be called when there is a message for a window to process like a click on a button.

nio
  • 5,141
  • 2
  • 24
  • 35
0

The answer to this question probably needs an explanation about the use/advantages of function pointers.

Case 1: Non blocking API's:

Lets say the objective is to develop a generic module that does some network download of a file and give the output buffer to the caller. In order for this objective, it makes more sense to provide the application with a non blocking API as follows

/* Prototype of the download callback passed as argument */
  typedef int (*app_download_cbk)(char *out_buffer, unsigned int ui_buffer_length);

/* Asynchronous API to download a file */
  int download_file(const char *URI, app_dwnload_callbask app_cbk);

In this case the application can call the API download_file and do its own operations. Whenever the download is complete, the module will notify the application via the callback that you registred during the API call for download_file

case 2: Generic implementation of a module

Lets say the objective is to develop a generic module that does some query of database and provides the data to the application. In this case it is up to the application to do whatever it needs. So you can design an API that accepts a function pointer. So the module can pass the query results via the callback and the application can process these results as needed.

Vivek Maran
  • 2,623
  • 5
  • 38
  • 52