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 ?
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.
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.
Like normal data pointers, we have function pointers that point to functions . The address of a function is stored in a function pointer.
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.
Sure. Simply go ahead and do it.
#include <stdio.h>
int main(void)
{
printf("%p\n", &main);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With