函数指针:
#include <iostream>
using namespace std;
void (*p)();
void edit(){
cout << "屌丝"<<endl;
}
void file(){
cout <<"逆袭" <<endl;
}
int main()
{
p=&edit;
(*p)();
p=file;
(*p)();
}
输出为:
屌丝
逆袭
由这个程序我们能看出来,那就是函数指针是指向函数的指针变量,就是指针变量。
p=edit 这个函数的标识符标号就是这个函数本身的地址,p=&edit也可以。结果是一样的。
函数指针就是把函数地址赋给函数指针,通过函数指针调用函数。也可以将函数地址传给函数指针作为参数使用。
函数指针的声明格式为:
指针函数:
指针函数就是返回地址给调用函数,
声明方式为:
函数类型 * 指针变量名(形参列表)
#include <iostream>
using namespace std;
int* p(){
static int a=0;
return &a;
}
int main()
{
cout<<p()<<endl;
cout<<*p()<<endl;
}
输出为:
0x804a0d8
0
返回的值是一个指向int型的变量的地址。
其他的不会。。。
准备睡觉,不好好睡觉会死人。