Here’s how to obtain a pointer to the main
function:
#define DECLARE_UNUSED( name ) (void) name; struct name
int main()
{
int(*ptr)() = &main;
DECLARE_UNUSED( ptr ); // Prevents using `ptr`.
// Don't use `ptr` here. In particular, don't call.
}
Note that
main
must have result type int
.
calling main
(e.g. via that pointer) incurs Undefined Behavior.
It is not necessary to return anything from main
; the default return value is 0.
As you can see main
is a very special function.
Those rules do not (in general) apply to other functions.
Also note that Visual C++ is wrong in not diagnosing void
result type.
Finally, note that writing non-standard void
is one character more to type than standard int
, i.e., it is just a very, very dumb thing to do. ;-)
PS: Visual C++ is probably mumbling things about int main
because it (probably) translates void main
to int main
internally, and probably it does that to make things link with a non-intelligent linker while actively supporting void main
so that e.g. Microsoft’s own non-standard examples in their documentation will compile. That’s my theory #1 anyway, since you ask. But it is, of course, pure guesswork, and it may be that even those who coded that up have no clear idea of why (theory #2).