Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 577
ArtStyle Avatar asked Feb 17 '15 17:02

ArtStyle


People also ask

What is the address of main function in C?

The address is the memory location where the entity is stored. Every block of code in the program has its own memory location in the program. Which means like any variable or object methods and functions also have memory address.

What is main () called in C?

In 'C' you can even call the main() function, which is also known as the "called function" of one program in another program, which is called "calling function"; by including the header file into the calling function.

Where is the address of a function stored in C?

Like normal data pointers, we have function pointers that point to functions . The address of a function is stored in a function pointer.

What does it mean Main in address?

home address , in relation to any person, means the address of his or her sole or main residence or, if he or she has no such residence, his or her most usual place of abode or, if he or she has no such abode, the place which he or she regularly visits; Email Address means a current valid email address.


1 Answers

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.

like image 134
Lightness Races in Orbit Avatar answered Nov 10 '22 03:11

Lightness Races in Orbit