当前位置:网站首页>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 .
边栏推荐
- Some websites of QT (software download, help documents, etc.)
- C/s model and P2P model
- BGP Federation +community
- Tensorflow1.14 corresponds to numpy version
- an error occurred while trying to rename a file in the destination directory code 5
- What are the bank financial products? How long is the liquidation period?
- Four kinds of hooks in deep learning
- Download address of QT source code of each version
- Visual studio tools using shortcut keys (continuous update)
- Module build failed: TypeError: this. getOptions is not a function at Object. stylusLoader
猜你喜欢

Onnx crop intermediate node

How to become a white hat hacker? I suggest you start from these stages

Tutorial (5.0) 02 Management * fortiedr * Fortinet network security expert NSE 5

20220606 Young's inequality for Matrices

BGP Federation +community

IP address introduction

JUC原子引用与ABA问题

How excel adds hyperlinks to some text in a cell

图数据库Neo4j介绍

类的加载概述
随机推荐
an error occurred while trying to rename a file in the destination directory code 5
教程篇(5.0) 03. 安全策略 * FortiEDR * Fortinet 网络安全专家 NSE 5
20211020 段院士全驱系统
Installation of sonarqube code quality management platform (to be continued)
How to resolve "the operation cannot be completed successfully because the file contains viruses or potentially junk software
Exporting MySQL data table documents using Navicat
Pytorch model tuning - only some layers of the pre training model are loaded
Top+jstack to analyze the causes of excessive CPU
20211006 积分、微分、投影均属于线性变换
JUC atomic accumulator
Collection of garbled code problems in idea development environment
[QNX hypervisor 2.2 user manual] 4.5 building a guest
20211018 一些特殊矩阵
Spectre record
20211018 some special matrices
20211006 linear transformation
Module build failed: TypeError: this. getOptions is not a function at Object. stylusLoader
What exactly is Huawei cloud desktop saying when it says "smooth"?
Onnx crop intermediate node
Neo4j环境搭建