We all know the fearsome syntax for defining function pointers and function types, the one with the confusing parenthesis. You can find lot's of examples on the net. But here's a refresher.
typedef int (foo_type)(int);
typedef int (*foo_ptr_type)(int);
And here's a type of a another function that returns a pointer to the above function:
typedef foo_ptr_type (foo_getter_type)(char*);
typedef foo_type* (foo_getter_type)(char*);
It is possible to unwrap above typedef, using a pretty obscure syntax(this is still pretty well known):
typedef int (*(foo_getter_type)(char*))(int);
Now can you guess how you would define a function that has the same type as int (*(foo_getter_type)(char*))(int)? No?
You just add brackets and give a name to char* argument:
int (*(foo_getter)(char* name))(int)
{
// I can't believe this thing compiled...
}
At first the function looks like it returns an int, but really it's returning a function that returns an int, go figure. I suspected this feature was there after I had a leftover "(int)" in my function declaration and compiler reported "cannot declare a function that returns a function". What? You can do that? So i started trying things out and discovered this syntax. I paged through K&R and couldn't find a mentioning that it's even possible, searched through net and could't find anything similar, so I guess this is one of the lesser known dusty corners of C that even language designers forgot they added it in there...