当前位置:网站首页>十七、C函数指针与回调函数
十七、C函数指针与回调函数
2022-07-22 07:33: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
边栏推荐
- 线上推广新方式,VR全景展示如何吸引用户的关注?
- [external sorting] merge ideas to complete external sorting
- Can flick SQL query Clickhouse
- Guys, when Flink SQL job submits a job to yarn, it reports an SQL error that cannot be executed. If it is executed locally, it does not report an error. The server
- Hcip OSPF interface network type experiment report
- DP subsequence series problem
- Deep learning (II) takes you to understand neural networks and activation functions
- 网络之物理层
- MySQL join and index
- 深说浅谈DOM对象,用4个版本demoplus让你扭断history.state头(更)
猜你喜欢
OSPF special area comprehensive experiment
On the dilemma faced by non transferable reputation points NFT SBTS
【计网】(三)超网、路由、NAT协议
UART communication experiment (query mode)
How does win11 close the touch pad? Three solutions for closing the touch panel in win11
Detailed explanation of inheritance
Problems in CPD registration
[external sorting] merge ideas to complete external sorting
Sqlmap is opened in the form of code rather than image
Data Lake (18): Flink and iceberg integrate SQL API operations
随机推荐
继承的详解
Bigder:37/100 一个误操作
Request message details (request header, get, post, request body)
为什么有些参数reload就可以生效,而有些参数必须重启数据库?
Chery Xingtu's product plan was exposed, and the 2.0T turbocharged engine was launched at the end of the year
MySQL JDBC programming
How does win11 run as an administrator? Win11 setup method running as Administrator
[MCU simulation project] external interrupts 0 and 1 control diodes through different triggering methods respectively
[HMS core] [FAQ] in app purchases FAQ sharing
SSRF vulnerability attack intranet redis recurrence
[external sorting] fast sorting idea completes external sorting
impdp content=data_only 当存在记录时可否选择跳过还是覆盖选项?
【HMS core】【FAQ】【Account Kit】典型问题集2
[HMS core] [Health Kit] [FAQ] collection of data subscription function questions
网络之物理层
线上推广新方式,VR全景展示如何吸引用户的关注?
Prepare for the attack and defense drill. Here is a security deployment map of Tencent!
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~
buu-misc进阶
PostgreSQL database is deployed on Linux server. Ms level is queried locally. Pgadmin installed on windows is used to query super slow for about 20s. Is it a network problem or a database configuratio