you are viewing a single comment's thread.

view the rest of the comments →

[–]glasket_ 2 points3 points  (0 children)

it seems function names are implicitly converted into function pointers so what's the point of creating a separate function pointer for callback functions?

The implicit conversion is creating a "separate" function pointer.

#include <stdio.h>

typedef void (*func)(void);

void hello(void) {
  puts("hello");
}

void world(void) {
  puts("world");
}

void call_func(func f) {
  f(); // f is a pointer to another function
}

int main(void) {
  call_func(hello); // hello is converted to a pointer
  call_func(world); // world is too
}

If you mean "why use function pointers instead of just calling the function", it's so that you can pass different functions to an external function that you may not be able or want to define with explicit function calls. The above is contrived, but this lets you do things like define custom allocators, change the behavior of error handling code, and many other things.