Address of function main() in C/C++

c, c++, function-pointers, pointers

Solution

C

Sure. Simply go ahead and do it.

#include <stdio.h>

int main(void)
{
   printf("%p\n", &main);
}

C++

It is not permitted to take `main`'s address so, for your purposes, there isn't one:

`[C++11: 3.6.1/3]:` The function main shall not be used within a program. [..]

However, in GCC you can take the same approach as you would in C, via a compiler extension:

#include <iostream>

int main()
{
   std::cout << (void*)&main << '\n';
}

You will receive a warning that this is not compliant.

Problem

Is there a way to find out the address of main() in C or C++ ? Since it is itself a function ,would there be an address of it own ?

Original source

Related problems