当前位置:网站首页>十七、C函數指針與回調函數
十七、C函數指針與回調函數
2022-07-22 17:56:00 【搖滾馬儂】
一、函數指針
- 定義:
函數指針是指指向函數的指針變量,其本質上是一個指針,只不過這個指針指向函數 - 聲明格式:
type (*name)(param_type)
- 注意:
函數指針應與被指向的函數參數類型,返回值相同 - 代碼實現:
#include <stdio.h> int max(int x, int y) { return x > y ? x : y; } int main(void) { // 聲明一個函數指針 int (*p)(int, int) = &max; int a, b, c, d; printf("Please input three numbers:\n"); scanf("%d %d %d", &a, &b, &c); d = p(p(a, b), c); printf("%d,%d,%d\n", a, b, c); printf("最大值:%d\n", d); return 0; }
- 運行結果:
[email protected]:/media/d102/EPAN/Desktop/c++_ubuntu/c++_01/workspace$ g cc test1.c -o test [email protected]:/media/d102/EPAN/Desktop/c++_ubuntu/c++_01/workspace$ ./test Please input three numbers: 11 22 33 11,22,33 最大值:33
二、回調函數
- 定義:
回調函數就是一個通過函數指針調用的函數 - 代碼實現:
#include <stdio.h> #include <stdlib.h> // 定義回調函數 // size_t:一種數據類型,相當於unsigned int類型,但是比其容量大 void populate_array(int *array, size_t arraySize, int (*getNextValue)(void)) { for (size_t i = 0; i < arraySize; i++) { array[i] = getNextValue(); } } // 獲取隨機值 int getNextRandomValue(void) { return rand(); } int main(void) { int myarray[10]; populate_array(myarray, 10, getNextRandomValue); for (int i = 0; i < 10; i++) { printf("%d ", myarray[i]); } printf("\n"); return 0; }
- 運行結果:
[email protected]:/media/d102/EPAN/Desktop/c++_ubuntu/c++_01/workspace$ ./test2 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421
边栏推荐
- Chery Xingtu's product plan was exposed, and the 2.0T turbocharged engine was launched at the end of the year
- Talking about DOM objects in depth, use four versions of demoplus to break the history.state header (more)
- [MCU simulation project] external interrupt 0 controls the light-emitting diode on and off
- How is VR panorama displayed in all walks of life? How to implement the application?
- OSPF special area comprehensive experiment
- Using dichotomy to find the elements of an array
- MySQL JDBC programming
- [database] addition, deletion, modification and query of MySQL table (Advanced)
- 所有navicat版本都支持MySQL吗,为什么我打开连接不上呢?
- How to make an appointment while watching the panorama? Here comes the VR catering system tutorial
猜你喜欢
Win11显示麦克风未插上怎么办?Win11显示麦克风未插上的解决方法
Chery Xingtu's product plan was exposed, and the 2.0T turbocharged engine was launched at the end of the year
Prepare for the attack and defense drill. Here is a security deployment map of Tencent!
Family Trivia
(十一)51单片机——用AT24C02实现存储秒表数据(附成果展示)
Talking about DOM objects in depth, use four versions of demoplus to break the history.state header (more)
使用 Abp.Zero 搭建第三方登录模块(三):网页端开发
[cloud native] docker deployment database persistence
数据湖(十八):Flink与Iceberg整合SQL API操作
【数据库】MySQL表的增删改查(基础)
随机推荐
ORACLE语句调整
网络之物理层
subprocess
MySQL系列文章之四:执行计划
[network counting] (III) hypernetwork, routing, NAT protocol
QWidget add right-click menu
Detailed explanation of bokeh parameter setting
C#静态类和静态类成员
How can VR panoramic display attract users' attention in a new way of online promotion?
【案例分享】配置IS-IS的路由渗透功能
MySQL join and index
异常的理解学习
Methodology (I): how to quickly learn a programming language
Conference OA project introduction & Conference release
Bigder:35/100 development colleagues said that I tested it myself. But something went wrong after the launch.
How is VR panorama displayed in all walks of life? How to implement the application?
What is the difference between win11 beta 22621.436 and 22622.436?
Install vscode offline
[database] addition, deletion, modification and query of MySQL table (basic)
【单片机仿真项目】 外部中断0控制发光二极管亮灭