当前位置:网站首页>C language: callback function
C language: callback function
2022-06-13 09:12:00 【Caixinzhi】
What is the callback function ?
A callback function is a function called through a function pointer . Put the function pointer ( Address ) Pass as argument to another function , When this function is used to call the function it points to , It's called a callback function . The callback function is not called directly by the function's implementer , It's called by another party when a particular event or condition occurs , Used to respond to the event or condition .
This concept is more abstract , Next , Through two implementation of the computer code comparison , To learn more about callback functions .
Purpose : The simulation realizes that the computer makes the operation of addition, subtraction, multiplication and division .
Before using the callback function to simulate the implementation of the computer , First use another method to help understand , Encapsulate functions that implement different functions , It is called in an array , This method is called using an array of function pointers to store different kinds of calculations . The first thing to know is , A function pointer is a pointer to a function , The array of function pointers is to store these pointers to functions in an array , Then call the required function by subscript in this array , This code is relatively simple , Don't explain in detail , Go straight to the code :
#include <stdio.h>
void menu()
{
printf("********************************\n");
printf("**** 1. Add 2. Sub ****\n");
printf("**** 3. Mul 4. Div ****\n");
printf("********** 0. exit ***********\n");
printf("********************************\n");
}
int Add(int x, int y)
{
return x + y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
int main()
{
int input = 0;
int x = 0;
int y = 0;
int ret = 0;
//pfArr Is an array of function pointers , Also called transfer table
int (*pfArr[5])(int, int) = { 0, Add, Sub, Mul, Div };
do
{
menu();
printf(" Please select :>");
scanf("%d", &input);
if (input == 0)
{
printf(" Exit the computer \n");
break;
}
else if (input >= 1 && input <= 4)
{
printf(" Please enter 2 Operands :>");
scanf("%d %d", &x, &y);
ret = (*pfArr[input])(x, y);
printf(" The result of the calculation is :%d\n", ret);
}
else
{
printf(" Input error , Re input \n");
}
} while (input);
return 0;
}Understand this code to achieve the computer , After you can roughly know how to implement , Then there is the second method : Use the callback function to call the required function for calculation .
This is a flexible method , First use in the main function switch Statement to determine the user's choice , Then pass the addresses of different functions to another function through different choices , Call the required function again through this address in this function , This is not called directly by the implementer of the function , It's called by another party when a particular event or condition occurs , The function used to respond to this event or condition is called a callback function , See below for the specific code :
#include <stdio.h>
void menu()
{
printf("********************************\n");
printf("**** 1. Add 2. Sub ****\n");
printf("**** 3. Mul 4. Div ****\n");
printf("********** 0. exit ***********\n");
printf("********************************\n");
}
int Add(int x, int y)
{
return x + y;
}
int Sub(int x, int y)
{
return x - y;
}
int Mul(int x, int y)
{
return x * y;
}
int Div(int x, int y)
{
return x / y;
}
void calc(int (*pf)(int, int))
{
int x = 0;
int y = 0;
int ret = 0;
printf(" Please enter 2 Operands :>");
scanf("%d %d", &x, &y);
ret = pf(x, y);
printf(" The result of the calculation is :%d\n", ret);
}
int main()
{
int input = 0;
do
{
menu();
printf(" Please enter :>");
scanf("%d", &input);
switch (input)
{
case 1:
calc(Add);
break;
case 2:
calc(Sub);
break;
case 3:
calc(Mul);
break;
case 4:
calc(Div);
break;
case 0:
printf(" Exit the computer \n");
break;
default:
printf(" Input error , Re input \n");
break;
}
} while (input);
return 0;
}This article introduces the concept of callback function through a simple computer code , Next, there will be an article on sorting algorithm , It will be used again 、 And get a little deeper into the callback function .
The use of callback function in actual code is flexible , This article is just a summary of its preliminary understanding , For reference only .
边栏推荐
- 20211104 为什么相似矩阵的迹相同
- Spectre record
- 20211005 Hermite矩阵及几个性质
- Simulink的Variant Model和Variant Subsystem用法
- 20211108 det(AB)=det(A)det(B)
- Neo4j environment construction
- 20220524 how to install coppeliasim to disk D
- 20211028 调节和跟踪
- ""? "& in URL Role of "" sign
- How many TCP connections can a machine create at most?
猜你喜欢

How Simulink adds modules to the library browser

Mttr/mttf/mtbf diagram

BGP Federation +community

Redis fuzzy query batch deletion

【 sécurité 】 comment devenir ingénieur de sécurité de 0 à 1 contre - attaque pour la Fondation zéro

Jfinal and swagger integration

20211104 为什么相似矩阵的迹相同
Drill down to protobuf - Introduction

Neo4j - CQL使用

20211104 why are the traces of similar matrices the same
随机推荐
QML(06)——qml. Add a new folder under QRC
Final principle
Jfinal and swagger integration
C/S模型与P2P模型
JUC 原子累加器
Some websites of QT (software download, help documents, etc.)
20211115 任意n阶方阵均与三角矩阵(上三角或者下三角)相似
How to become a white hat hacker? I suggest you start from these stages
20211104 why are the traces of similar matrices the same
JUC原子引用与ABA问题
transforms. ColorJitter(0.3, 0, 0, 0)
Longadder of the source code of JUC atomic accumulator
JUC原子整数
20211006 积分、微分、投影均属于线性变换
20211108 observable, controllable, stable and measurable
20211028 adjustment and tracking
JUC atomic array
Neo4j environment construction
20211028 调节和跟踪
20211115 矩阵对角化的充要条件;满秩矩阵不一定有n个线性无关的特征向量;对称矩阵一定可以对角化