In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the function pointer array in detail. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Last time I encountered such a problem in the process of developing a certain software, the previous module sent me binary data, the input parameters are char* buffer and int length,buffer are the first address of the data, and length indicates the length of the data. The characteristics of data are variable length and type.
The type of the data is identified by * * bytes (buffer [0]), with a total of 256 (28) possibilities. My task is to deal with every possible data type, and my module contains several functions, each of which is similarly handled. If you follow the usual practice, the following code will be written:
Void MyFuntion (char* buffer, int length) {_ _ int8 nStreamType = buffer [0]; switch (nStreamType) {case 0: function1 (); break; case 1:. Case 255: function255 (); break;}
If I continue to write in this way, I have to make so many judgments in every function, the code must be very long, and each time I have to make many judgments before finding the correct processing function, the execution efficiency of the code is not high. In view of the above problem, I came up with the method of using function pointer array to solve this problem.
The concept of function pointer has been mentioned in Mr. Tan Haoqiang's classic C language programming tutorial, but in most cases we can not use it and ignore its existence. A function name is actually a pointer pointing to the entry address of a function, but it is different from ordinary pointers such as int* and double*. See the following example to understand the concept of a function pointer:
Void MyFuntion (char* buffer, int length) {_ _ int8 nStreamType = buffer [0]; switch (nStreamType) {case 0: function1 (); break; case 1:. Case 255: function255 (); break;}
Statement 1 defines a function function that inputs two integers and returns an integer (input parameters and return values can be any other data type); statement 3 defines a function pointer, which, unlike the int* or double* definition pointer, must also indicate the input parameter, indicating that it is a function pointer, and * fun must also be enclosed in a pair of parentheses
Statement 6 assigns the function pointer to funtion, provided that the input parameters and return values of * fun and function are the same. Statement 5 calls the function function () directly, and statement 7 is the pointer to the calling function, which is equivalent. Of course, from the above examples do not see the advantages of function pointers, the main purpose is to introduce the concept of function pointer array. We can see from the above example that since the function name can be saved through the function pointer.
You must also be able to define an array to hold several function names, which is the function pointer array. The prerequisite for the correct use of the function pointer array is that several functions that need to be saved through the function pointer array must have the same input and output values.
In this way, the problems I face in my work can be solved as follows:
Void MyFuntion (char* buffer, int length) {_ _ int8 nStreamType = buffer [0]; switch (nStreamType) {case 0: function1 (); break; case 1:. Case 255: function255 (); break;}
As long as 2 lines of code, it completes the work of 256 case statements, reduces the workload of writing code, takes nStreamType as the array subscript and calls the function pointer directly, which is also more efficient than case statements in terms of code execution efficiency. If you want to do this in multiple functions, the function pointer array can better reflect its advantages.
This is the end of the article on "how to correctly use the function pointer array". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.