by femto113 · 8y ago · 0 comments
typedef int func(int arg);
int foo(int a) { return a * 10; }
...
func *f = foo;
...
int x = f(10);This pattern is common in well-structured applications, and is especially useful for clarifying external and internal APIs. e.g. from the Dovecot IMAP server:
typedef bool event_callback_t(struct event *event,
enum event_callback_type type,
struct failure_context *ctx,
const char *fmt, va_list args);
You can now define a function pointer or declare a parameter using event_callback_t. This is both concise and intention revealing.The way to read a typedef (in general) is when you strip off the word "typedef", instead of declaring a type, it declares a value of that type. This applies to all types, not just function types. For example, an array of 5 ints is declared as
int foo[5];
So the type of all 5-element arrays is defined by typedef int FiveLongIntArray[5];