函数指针基础

知识点

如果调用函数名,就是调用函数在内存中的首地址。
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");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值