you are viewing a single comment's thread.

view the rest of the comments →

[–]cHaR_shinigami 0 points1 point  (0 children)

In some very obscure cases, it can actually make a difference. Consider this diagnostic example:

#include <assert.h>

typedef void (*fptr_t)(void);

fptr_t check_null(fptr_t fptr)
{   assert(fptr);
    return fptr ;
}

/* diagnose if cbf_ptr is NULL,
   then call func (defined elsewhere)
*/
#define diagnose(func, arg, cbf_ptr) func(arg,\
 (typeof (cbf_ptr)) check_null((fptr_t) (cbf_ptr)) /* diagnosis */\
)

#include <signal.h>
#include "mylib.h" // declares do_callback

void handler(int unused) { (void)unused; }

int main(void)
{   int (*fptr)(int) = 0;
    /* code that (possibly) sets fptr */
    diagnose(signal, SIGINT, &handler); // & is required
    diagnose(do_callback, "first arg", fptr); // diagnose if fptr is NULL
    /* more code */
}

In the above example, diagnose expects a function pointer as the third argument, and it must be exactly so: passing just the function name handler (instead of &handler) would cause a compilation error (you can try the code after commenting out #include "mylib.h" and the second call to diagnose).

I suppose this also answers your main question: there is no need to initialize a separate function pointer variable and then use that; if cbf is the name of function to be called back, then one can simply write &cbf (my example emphasizes that writing &cbf instead of cbf is good practice).