知识点
如果调用函数名,就是调用函数在内存中的首地址。
int test(int a)//参数要和函数匹配
{
printf("a%d\n", a);
return 0;
}
void main()
{
typedef int(*pm)(int);
pm myp = NULL;
test(2);
myp = test;
myp(10);
}
这里我们看是不是和我们以前学习的int a = 10;int *p = NULL; p = &a;无非这里换成了函数。
如果大家把这一点想通了,其实他们本质上是一样的。
代码
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef int(myFunc)(int);
//定义一个函数类型,函数名代表函数的首地址
typedef int(*pm)(int);
int test(int a)//参数要和函数匹配
{
printf("a%d\n", a);
return 0;
}
void main()
{
myFunc *myfunc;//一个函数指针
test(10);//变量,函数的名字就是函数的首地址
//===========>int a = 0;int *p = NULL; p = &a *p = 100;
myfunc = test;//取函数的首地址赋值给函数指针
myfunc(2);//这个时候函数指针就能取到函数的地址并且可以通过修改参数而改变函数内属性数据
//有了这个我们就可以通过修改函数,这样外挂就可以做成
{
pm myp = NULL;
myp = test;
myp(10);
}
{
//直接定义一个函数指针变量
int(*pf)(int);//==typedef int(*pm)(int)
pf = test;
pf(20);
}
system("pause");
}