当前位置:网站首页>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 .

原网站

版权声明
本文为[Mortal programming biography]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010512215227.html