BartC said:
I have a C code generator that likes to insert lots of parentheses in
type declarations (rather than truly understand how they work).
But when declaring a function returning a pointer:
int (*fn) (void);
That's not what the above does. It declares (and, at file scope,
tentatively defines) a pointer to a function.
int (*fn) (void ) {return NULL;}
Returning NULL is peculiar but fits with what you say you intended.
the prototype works, but not the definition (it's a syntax error). How
do I have to change the latter to make it compile?
The latter, just like the former, declares a pointer, not a function.
You'd make it work by giving it a suitable initialiser:
int (*fn)(void) = f; /* for some f of the right type, of course */
but it seems that you intended to write a prototype and a definition of
a function that returns a pointer:
int *fn(void);
int *fn(void) { return NULL; }
Everywhere else the extra parentheses don't seem to be a problem.
They aren't a problem here either, it's just that you've mistaken what
they mean.
There *is* a symmetry problem with C's function definitions: you can use
a typedef of a function type to declare functions but not to define
them. For example, if you want to declare a whole bunch of functions of
the same type, it's easy:
typedef int binary_operator(int, int);
binary_operator add, sub, mul, div, rem, mod, exp;
and the typedef comes in hand if you have (as is likely) a table of
pointers to such functions. But when you come to define them, you can't
use the typedef name -- you have to repeat the types:
int add(int a, int b) { return a + b; }
It would be nice if you could use the typedef and add just the parameter
names. However, the "obvious" syntax is wrong:
binary_operator add(a, b) { return a + b; }
because binary_operator look like (well, is) the return type and not the
function type. You'd need a new form of function definition, maybe:
binary_operator add = (a, b) { return a + b; }
which, at least, looks like an initialiser. Far too fiddly to parse, I
imagine, and the use cases are too rare to register on the committee's
radar.