2020.9.25 第11节 指针终结篇

2020.9.25 第11节 指针终结篇

一、函数指针

函数指针: 指向函数的指针

1.定义一个函数指针?

方法: 用(*指针名)替换函数名,剩下东西照着函数原型写即可

2. 函数指针的赋值

用函数名赋值 两种

3.函数指针如何调用函数

3.1 只用指针名替换函数名

3.2 *指针名替换函数名

4.函数指针使用

4.1 一般都是充当函数参数使用—>回调函数

#include <stdio.h>
int max(int a, int b) 
{
	return a > b ? a : b;
}
int maxC(int a, char b) 
{
	return a > b ? a : b;
}
int Min(int a, int b) 
{
	return a > b ? b : a;
}

int sum(int a, int b) 
{
	return a + b;
}


void printNum(int a, int b) 
{
	printf("%d", a + b);
}
void print(int (*pF)(int, int), int a, int b)  //pF=max ,pF=sum
{
	printf("%d\n", pF(a, b));
}


int main() 
{
	int (*pFunction)(int a, int b) = NULL;
	pFunction = max;
	//pFunction = maxC;	//warning C4028: 形参 2 与声明不同
	printf("%d\n", pFunction(1, 2));
	printf("%d\n", (*pFunction)(1, 2));
	int (*pFun)(int , int ) = NULL;
	pFun = &max;
	printf("%d\n", (*pFun)(1, 2));
	printf("%d\n", pFun(1, 2));
	//arning C4133: “=”: 从“void (__cdecl *)(int,int)”到“int (__cdecl *)(int,int)”的类型不兼容
	//pFun = printNum; 错误写法,类型一致
	//回调函数设计思想
	//返回值和参数类型在函数指针的赋值上面必须一致
	print(max, 1, 2);	//打印
	print(sum, 1, 2);	//打印
	print(Min, 1, 2);
	//int (*pF)(int, int)  pF=max  pF=sum
	return 0;
}

二、typedef与函数指针

1.方法: 用(*指针名)替换函数名,剩下东西照着函数原型写即可

2.函数指针可以用typedef简化代码

#include <stdio.h>
int max(int a, int b) 
{
	return a > b ? a : b;
}
//方法: 用(*指针名)替换函数名,剩下东西照着函数原型写即可
//
void printA(int (*pF)(int, int), int a, int b) 
{
	printf("%d\n", pF(a, b));
}
// 函数指针可以用typedef简化代码
typedef int (*pF)(int, int);
void printB(pF max, int a, int b) 
{
	printf("%d\n", max(a, b));
}
typedef void (*pFF)(int(*)(int, int));
void printC(pFF printF, pF max, int a, int b) 
{
	printF(max, a, b);
}

void printAA(void (*pFF)(int(*)(int, int)), int (*pF)(int, int),int a, int b)
{
	pFF(pF, a, b);
}

int main() 
{
	//1.基本用法 给一个类型起小名
	typedef int 整数;
	int iNum = 1;
	整数 iNumber = 2;
	//2.给数组起别名
	typedef char STR[10];  //char str[10]
	STR str;			   //char str[10]
	gets_s(str, 10);
	puts(str);
	//3.函数指针可以简化代码
	void (*ppp)(int (*)(int, int), int, int) = NULL;
	typedef void (*YYKK)(int (*)(int, int), int, int);
	YYKK pYk = NULL;
	typedef int INT[2];		//int  array[2][2]
	INT array[2];
	//4.结合结构体使用
	return 0;
}

三、const与函数指针

#include <stdio.h>
void print(const char* fileName) 
{
	printf("%s", fileName);
}
void printA(const int a) 
{
	//当前函数中不能修改const值
	//a++; 可以使用 但是可能修改
	printf("%d\n", a);
}
int main() 
{
	//前两种没有区别
	//const修饰的是指针所指向的类型
	const int* p1 = NULL;
	int const* p2 = NULL;
	const int cNum = 0;
	int iNumber = 1;
	p1 = &cNum;
	//(*p1)++;  cNum常属性
	p2 = &iNumber;
	//(*p2)++;  导致指针不能修改所指向的数据

	//const: 常属性 常量的属性-->只读,不可修改
	print("fileName");
	int* const p3 = &iNumber; //修饰的是指针变量,指针变量的值不能改变
	iNumber = 1001;
	printf("p3=%d\n", *p3);
	const int* const p4 = NULL;


	return 0;
}

四、万能函数充当函数指针

万能指针充当函数指针:

1.必须强制类型

2.必须采用*指针名的调用方式

// (*(void(*)()))()
/*
	万能指针充当函数指针:
	1.必须强制类型
	//2.必须采用*指针名的调用方式
*/
#include <stdio.h>
int sum(int a, int b) 
{
	return a + b;
}
void print() 
{
	printf("ILoveyou\n");
}
int main() 
{
	void* pVoid = sum;
	printf("%d\n", ((int(*)(int,int))pVoid)(1, 2));
	typedef int(*PP)(int, int);
	printf("%d\n", ((PP)pVoid)(1, 2));
	// (*(void(*)())p)()
	void* p = print;
	//(*p)();
	//函数指针类型:void(*)()
	(*(void(*)())p)();
	return 0;
}

五、复杂的指针

如何识别复杂的指针

右左法则?

	如何识别复杂的指针
		右左法则?
		1.找标符符 Fun
		2.往右找)
		3.往左找(
		4.(*fun)
			int (*fun)(int *)
		指向指针函数的数组指针
		int (*(*fun)(int *))[5]
		int (*fun)(int *,int (*)(int *))
		int(*(*pfun())())()
		int (*fun)(int *,int (*)(int *))

        
#include <stdio.h>
int print(int* p) 
{
	printf("%d", *p);
	return *p;
}

int printA(int(*p)(int array[], int), int a, int b) 
{

}

int main() 
{
	int num = 100l;
	print(&num);
	void (*pp)(int(*)(int[], int), int, int) = printA;
	return 0;
}

六、多文件

一个.c文件对应一个.h文件 避免因头文件嵌套包含导致重定义问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值