当前位置:网站首页>C callback function, interface function pointer as function parameter, function pointer as structure member
C callback function, interface function pointer as function parameter, function pointer as structure member
2022-07-28 14:52:00 【Love the west wind】
First statement , This is C Usage of ,C++ We should put an end to it ;
There are two common uses of function pointers :
1. One is as a member of a structure ;
2. The function pointer is used as the parameter of the function ;
The meaning of the two methods is interface ,C It's also called Callback function ;
Function pointer declaration :
typedef int (*objFunction_ptr)(int inValue) ;
// Callback function ( Function implementation )
int objFunction(int inValue)
{
// You want to adjust the parameters How to deal with ; This similar interface ; It may also be the result returned ;
int cur= inValue+10;
return cur;
}
One 、 Function pointers act on structural members
// Structure
typedef struct FounctionStruct{
int a;
objFunction_ptr callback;
}FOUND_STRUCT;// Structure passes in function
int GetMessage(int curValue,FounctionStruct & func)
{
curValue+= func.a;
return func.callback(curValue);
}// Test code
int main(int argc, char *argv[])
{
int curValue = 100;
FounctionStruct func;
func.a = 20;
func.callback = &objFunction;
int tar = GetMessage(curValue,func);
return 0;
}result tar =130;
That's it , Callback ; So you can get , The result returned , You can also weight the results , Encrypted , Something about yourself ;
Two 、 Function pointer as Function parameter
// function :
int GetMessageFun(int curValue,objFunction_ptr callback)
{
curValue += 100;
return callback(curValue);
}// Call tests ;
int main(int argc, char *argv[])
{
int curValue = 100;
objFunction_ptr fun = & objFunction;
int tarValue = GetMessageFun(curValue,fun);
return 0;
}result
tarValue=210;
That's it , Callback ; So you can get , The result returned , You can also weight the results , Encrypted , Something about yourself ;
边栏推荐
- 1st pre class exercise
- JS instantiation method
- Swiftui layout - size (bottom)
- 2022 melting welding and thermal cutting examination questions and online simulation examination
- OKR and grad
- The third pre class exercise
- 用 Table 在 SwiftUI 下创建表格
- Redis-持久化
- 468 product planning and promotion plan (150 copies)
- linux安装mysql
猜你喜欢

使用Weka与Excel进行简单的数据分析

@Solution to DS ('slave') multi data source compatible transaction problem

Redis-配置文件讲解

On July 29, apachecon | apachepulsar's exploration and practice in vivo will be broadcast soon

树莓派基础 | 总结记录树莓派学习过程中的一些操作

Another way of understanding the essence of Hamming code

2022 melting welding and thermal cutting examination questions and online simulation examination

How to reduce the resolution of only 3D camera but not UI camera

linux安装mysql

Floating point data type in C language (did you learn to waste it)
随机推荐
Redis-持久化
Installing redis in Linux
SwiftUI 的动画机制
卡方分布和伽马函数(Chi-Square Distribution)
Excel VBA password free view VBE encryption code
Swiftui layout - size (top)
ScottPlot入门教程:获取和显示鼠标处的数值
Hand in hand from 0 to a "Nuggets special attention" Google plug-in, 5000 words detailed vue3 responsive principle, the advantages, disadvantages and choices of several cache read-write schemes, flyin
Node文件操作
八、picker用法 下拉框选择效果
基础架构之日志管理平台及钉钉&邮件告警通知
SwiftUI 布局 —— 尺寸( 上 )
Redis persistence
@DS('slave') 多数据源兼容事务问题解决方案
面试官:ThreadLocal使用场景有哪些?内存泄露问题如何避免?
数字化转型安全问题频发,山石网科助力数字政府建设
How many ways can multithread run in sequence?
How to effectively conduct the review meeting (Part 1)?
Chi square distribution and gamma function
多商户商城系统功能拆解17讲-平台端订单列表