C/C ++中函数main()的地址 [英] Address of function main() in C/C++
本文介绍了C/C ++中函数main()的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以找到C或C ++中 main()的地址? 由于它本身是一个函数,是否会有它自己的地址?
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 ?
推荐答案
C
好的.只需继续进行即可.
C
Sure. Simply go ahead and do it.
#include <stdio.h>
int main(void)
{
printf("%p\n", &main);
}
C ++
不允许使用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]:
不得在程序内使用main函数. [..]
[C++11: 3.6.1/3]:
The function main shall not be used within a program. [..]
但是,在GCC中,您可以通过编译器扩展采用与C中相同的方法:
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.
这篇关于C/C ++中函数main()的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文