当前位置:网站首页>C secret arts script Chapter 2 (detailed explanation of pointers) (Section 3)
C secret arts script Chapter 2 (detailed explanation of pointers) (Section 3)
2022-06-12 14:24:00 【Mortal programming biography】
Previous section , We have basically introduced most of the pointer knowledge , There are still some important contents to be introduced , Don't talk much , Direct to the text .
One 、 A pointer to an array of function pointers
It's essentially a pointer , General form e.g :int(*(*prefer)[5])(int,int), This kind of dolls can go on indefinitely , For example, after the first level pointer passes, there are two-level pointers and three-level pointers , Until n Level pointer , But it is unnecessary and makes the code less readable , So we should avoid this kind of writing when it is not necessary .
Two 、 Callback function
1. Concept : A callback function is a function called through a pointer . If you put a pointer to a function ( Address ) Pass as argument to another function , When this pointer is used to call the function it points to, we call it a callback function , The callback function is not called directly by the function's implementer , It is called by another party when a specific condition or condition occurs , Used to respond to the event or condition .
2. Basic use of callback function
#include<stdio.h>
void print_1(char *str)
{
printf("hehe:%s",str);
}
void test(void(*p)(char*))
{
printf("test:\n");
p("hello world");
}
int main()
{
test(print_1)
return 0;
}analysis : The main function main call test Function and will print_1 Function the address of this function is passed to test function , And define test Function parameter refers to the function pointer print_1 Page address , Through this function pointer to print_1 Quoting print_1 function , Final output hehe:hello world.
This function that calls a function by passing an address to another function is called a callback function , This mechanism is called a callback mechanism
In fact, this example is not appropriate for callback functions , After we have a deeper knowledge, it will be much easier for us to go back and understand this special function .
3、 ... and 、void* The pointer
We know in the chapter of pointers that there are char*,int*,float* And so on , But this void* It's the first time I've seen , What is his use , Let's study it carefully .
1. View of : understand void* This special pointer will tell us what we just touched on the pointer .
Pointer types : We know that a pointer can store the address of any type of data , But the pointer type determines how many bytes the pointer can access at a time . Like it int Type data is placed in char* In the pointer of type, when the pointer is accessed , Because it is char* So the pointer to can only access one byte at a time , This leads to a great reduction in the efficiency of access calls , So we will store it in the pointer corresponding to the data type according to the data type .
Knowledge Links : So this is with us void* What does a pointer matter ? Actually void* The purpose of this pointer is to store any type of address , Then someone wants to say : Isn't this the same as other pointers , Other pointers can also store any type of address ! Don't worry. , I haven't finished speaking , Although it is true that other types can store any type of address, they will not report an error when accessing the address , and void* Sure . Some people wonder again : It will cause error reporting and feel good ? juvenile , Not too small , Think carefully , If once you call your pointer when passing parameters, your data type is different from your pointer type , And your program will still run , If the result is incorrect because of this error in a project, you have to find the reason for it for a long time . So if you use void* The pointer will report an error , It is not a maintenance ? And when you program, you don't know what type of pointer to use to receive addresses , First use void* The pointer accepts and then casts , have a retinue before and behind , Don't you kill two birds with one stone ?
Add :void* An error will be reported during the interview , Because it is an indeterminate pointer type , If the type is uncertain, the access bytes are uncertain , Result in an error .
end : Okay , Fellow Taoists, this chapter is over , If you have any questions or what I said is wrong, please stay in the comment area or send me a private message , We have a long way to go , I'll see you later , Thank you for your support .
边栏推荐
- 初学者入门阿里云haas510开板式DTU(2.0版本)--510-AS
- Llvm 13.1 new pass plug-in form [for win]
- Unhandled exception stack overflow
- Player practice 15 xdemux and avcodecparameters
- Why do Chinese programmers change jobs?
- Communication flow analysis
- Codeforces Round #798 (Div. 2)(A~D)
- Knowledge of wireless card driver
- Running phase of SystemC
- OAuth2学习中的一些高频问题的QA
猜你喜欢

拆改廣告機---業餘解壓

Socket model of punctual atom stm32f429 core board

什么是自动出价?它的优势是什么?

Introduction to database system (Fifth Edition) notes Chapter 1 Introduction

Player actual combat 13 create qtopengl project to promote window control and reload qoopenglwedge

Player actual combat 25 unpacking module add close
![[ROC] aspriseocr C # English, Digital identification (not Chinese)](/img/80/198145df663d2eeec6b8b1d7bc47b6.png)
[ROC] aspriseocr C # English, Digital identification (not Chinese)

2022版Redis数据删除策略

公司运营中更注重转化的出价策略,如何实现? —Google sem
![[early knowledge of activities] list of recent activities of livevideostack](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[early knowledge of activities] list of recent activities of livevideostack
随机推荐
高考回忆录
正点原子STM32F429核心板的插座型号
G++ error in compiling Win32 program: undefined reference to '__ imp_ GetStockObject‘
华为设备配置BGP AS号替换
Vs2012: cannot assign a value of type 'char *' to an entity of type 'lpwstr'
Two methods of QT using threads
SystemC uses SC_ report_ Handler processing log printing
969. pancake sorting
Postgresql14 installation and use tutorial
SystemC time
Dismantle and modify the advertising machine - Amateur decompression
Reverse order of Excel
注重点击,追求更多用户进入网站,可以选择什么出价策略?
Codeforces Round #798 (Div. 2)(A~D)
Perfect ending | detailed explanation of the implementation principle of go Distributed Link Tracking
Player practice 17 xvideowidget
华为设备配置OSPF伪连接
chrome://tracing Performance analysis artifact
完美收官|详解 Go 分布式链路追踪实现原理
PMP敏捷知识点