什么是 C 或 C++ 中函数的地址?


函数是定义在程序中执行特定操作的代码块。它用于简化程序员的工作,方法是定义一个经常出现的代码块以便在需要时重用它。

地址是实体存储的内存位置。程序中的每个代码块在程序中都有自己的内存位置。这意味着如同变量或对象方法和函数也具有内存地址。

若要获取函数的内存地址,你需要使用该方法的指针,并编写不带括号的函数名称。

示例

 实战

#include<iostream>
using namespace std;
void myfunc(){
   cout<<"This is myfunc";
}
int main(void){
   printf("address of function main() is :%p\n", main);
   printf("address of function myfunc() is : %p\n", myfunc);
   return 0;
}

输出

address of function main() is :0x40079d
address of function myfunc() is : 0x400787

更新于:19-9 月 2019

2 千+ 浏览

开启您的职业生涯

完成课程以获得认证

开始学习
广告
© . All rights reserved.