C语言基础之指针高级
指针高级
函数指针
-
指向函数的指针变量
-
定义:用(*指针名) 替换函数名,形参的名字可以省略
int Max(int a,int b) { return a>b?a:b; } //定义 int (*p)(int ,int )=NULL;
-
函数名就是函数指针 ,&函数名 也可以充当函数指针
-
-
函数指针的使用
-
函数指针使用必须所有类型一致,除了函数名
-
调用函数
#include <stdio.h> #include <windows.h> int Max(int a, int b) { return a > b ? a : b; } int main() { int* p = NULL; //int (*pMax)(int a, int b) = NULL; int (*ppMax)(int, int); //一般形参名都会省略掉 ppMax = NULL; //赋值方式 ppMax = Max; ppMax = &Max; //直接用函数指针替换函数名调用即可 printf("%d\n", ppMax(1,2)); printf("%d\n", (*ppMax)(1, 2)); //解引用调用 return 0; }
-
当做函数参数:回调函数
int Max(int a, int b) //重写print函数指针 { return a > b ? a : b; } int Sum(int a, int b) //重写print函数指针 { return a + b; } int Min(int aa, int bb) //重写print函数指针 { return aa > bb ? bb : aa; } //以函数指针充当函数参数 void print(int(*pMax)(int, int), int a, int b) { printf("%d\n", pMax(1, 2)); } //函数指针充当函数参数 //回调函数: 统一接口 print(Max, 1, 2); //最大值 print(Sum, 1, 2); //求和 print(Min, 1, 2); //求最小值
-
当做函数返回值
- 基本不会用二级有可能会考
- 常用的是调用函数,充当函数参数
typedef int(*PFUNC)(int, int); //函数指针充当函数参数 PFUNC returnFunction(PFUNC pf, int a, int b) { printf("%d\n", pf(a, b)); return pf; } PFUNC pf = NULL; pf = returnFunction(Max, 1, 2);
-
typedef基本用法用法
-
给类型起别名
-
给数据类型起别名
typedef int Int; int iNum = 1; Int iNum2 = 2;
-
-
给数组起别名
typedef int ARRAY[3]; ARRAY array; //int array[3] for (int i = 0; i < 3; i++) { array[i] = i; printf("%d\t", array[i]); } printf("\n");
typedef语句解析
-
直接用typedef起的别名所定义的名字替换typedef起的别名
-
去掉typedef就是所定义的东西
typedef int ARRAY[3]; ARRAY array; //typedef int ARRAY[3];----->typedef int array[3]; // int array[3]; ARRAY num[3]; //typedef int num[3][3] //int num[3][3] //函数指针 int Max(int a, int b) { return a > b ? a : b; } typedef int(*PFUNC)(int, int); PFUNC pf = NULL; //typedef int(*PFUNC)(int, int);----->typedef int(*pf)(int, int); //int(*pf)(int, int);
函数指针升级版----复杂函数指针
-
万能指针充当函数指针
-
使用前必须做类型转换
void print() { printf("空类型的指针充当函数指针\n"); } void* pVoid = NULL; //使用前必须做类型转换 pVoid = print; //print的类型: void(*)(); ((void(*)())pVoid)(); (*(void(*)())pVoid)();
-
-
以函数指针为参数的函数指针为参数的函数
-
void(p)(); 去掉变量名剩下就是指针: void()()
-
解析过程
-
void printData(int(*pMax)(int, int), int a, int b)
-
✳加指针名替换函数名
void printData(int(*pMax)(int, int), int a, int b) void (*p)(int(*pMax)(int, int), int a, int b)
-
去掉函数形式参数
void(*p)(int(*)(int, int),int,int)
-
int Max(int a, int b) { return a > b ? a : b; } int Sum(int a, int b) { return a + b; } void printData(int(*pMax)(int, int), int a, int b) { printf("%d\n", pMax(a, b)); } void userPrint(void(*p)(int(*)(int, int),int,int), int(*pMax)(int, int), int a, int b) { p(pMax, a, b); } userPrint(printData, Max, 1, 2); userPrint(printData, Sum, 1, 2); //* + 指针名替换函数名,去掉形参 void (*pFunc)(void(*)(int(*)(int, int), int, int),int(*)(int, int),int, int) = NULL; //typedef int(*B)(int, int); //typedef int(*C)(int, int); //typedef void(*A)(B b, int, int); void (*pFunc)(A a,C c,int, int) = NULL; pFunc = userPrint; pFunc(printData, Sum, 100, 200);
-
-
-
函数指针数组
int (*pMax)(int, int) = NULL; int(*pArray[2])(int, int); pArray[0] = Max; pArray[1] = Sum; for (int i = 0; i < 2; i++) { printf("%d\n",pArray[i](100, 200)); }
本文来自博客园,作者:{oy},转载请注明原文链接:https://www.cnblogs.com/Oysen/p/17005608.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)