int main(void) { int demo(); /*...*/ }; int demo(){ /*...*/ }
is equivalent to
int main(void) { extern int demo(); /*...*/ }; int demo(){ /*...*/ }
It declares the demo function with external linkage, inside the main function.
The demo
identifier will be valid till the end of the scope, but demo
will be a
normal, external function.
Unfortunately, you cannot do this for functions that are static
, because
the standard (http://port70.net/~nsz/c/c11/n1570.html#note30) makes things like
int main(void) { static int demo(); } static int demo(){ /*...*/ }
illegal.
As to why (*demo)();
works and is in this context equivalent to demo();
or (&demo)();
or (*&demo)();
:
After you declare demo with int demo();
, demo
is a function designator (which is not the same as a function pointer, not unlike an array designator is not the same as a pointer the the array's first element). Like arrays, though, function designators almost always decay to function pointers (see 6.3.2.1p4) and this type of decay happens in function calls too. Essentially demo();
decays demo
to a pointer and then calls
the function pointer. Applying *
to such a pointer yields again the function designator which again decays to a pointer to make the call.
Admittedly, all this stuff is very weird and esoteric and I can't even think of a situation where the distinction is useful. IMHO, it's probably best to stick to demo();
or (demo)()
(if you want to supress macro expansion) and never use (*demo)()
(at least not on functions -- on function pointers it is at least somewhat informative) and especially not (&demo)()
as it serves little purpose other than to confuse the reader.