Declaration (a) (array of function pointers, probably serving as some kind of dispatch table) is not unheard of. The others are not very common and if anything like them occurs, it will likely use typedef
names.
I wrote some code long ago in which there were some character arrays defined as typedef names, and these were passed into and returned from functions:
typedef char fixed_string[256];
fixed_string *fun(fixed_string *);
If we don't use a typedef
for the function declaration, it looks like:
char (*fun(char (*)[256]))[256];
Using arrays this way is inadvisable; the non-typedef
version is hard to read and the typedefs
are a leaky abstraction, because the arrays still undergo conversion to pointers. I wouldn't write that today.
Fixed arrays that are passed around are best wrapped up into a struct
:
struct fixed_string { char s[256]; };
we can pass and return that by value, or using pointers.
The (d) case:
void (*x(int, void (*y) (int))) (int);
can be rewritten with typedefs
like this:
typedef void (*action)(int);
action x(int, action);
This hints at pattern that can occur in some kinds of state machines. Maybe x
calls that action
, passing it the int
parameter, and then returns a pointer to another function that serves as the next action
.