2 ms·
Function pointers are easy if you typedef the function type. What a pointer to function (int) returning int? The difficult way: int (*p)(int); The easy
by Arch-TK 2y ago
Function pointers are easy if you typedef the function type.
What a pointer to function (int) returning int?
The difficult way:
int (*p)(int);
The easy way:
typedef int Func(int);
Func *p;
The typedef reads just like a normal function declaration except for the name is the type name rather than the function symbol name. Just like a function declaration, you can add more documentation by giving the parameters names. e.g.:
// used for some hypothetical allocator API
typedef void *Alloc(size_t size);
Similar trick can be used for pointers to arrays (just typedef the array type). Although in practice pointers to arrays just make for awful code.