Use of function pointer arrays in C ++

Source: Internet
Author: User

I encountered such a problem during the development of a software. The previous module sent binary data to me. The input parameter is char * buffer and INT length, and buffer is the first data address, length indicates the length of the batch of data. The data is characterized by an indefinite length and type. The first byte (buffer [0]) identifies the data type, which is 256 (28) in total.

My task must process each possible data type, and my module contains several functions, which must be processed similarly in each function. Write the following code as follows:

Void myfuntion (char * buffer, int length)

{__ Int8 nstreamtype = buffer [0];

Switch (nstreamtype)

{Case 0:

Function1 ();

Break;

Case 1:

......

Case 255:

Function255 ();

Break;

}

If you write this method, you must make so many judgments in each of my functions. The code to be written must be very long, and each processing is performed, the correct processing function must be found after many judgments, and the code execution efficiency is not high. To solve the above problem, I thought of using the array of function pointers to solve this problem.

The concept of function pointer is mentioned in the classic tutorial of C language programming by Mr. Tan haoqiang. In most cases, we cannot use it and ignore its existence. The function name is actually a pointer pointing to the function's entry address, but it is different from common pointers such as int * and double *. Let's look at the following example to understand the concept of the function pointer:

1 int funtion (int x, int y );

2 void main (void)

{

3 int (* Fun) (int x, int y );

4 int A = 10, B = 20;

5 function (A, B );

6 fun = function;

7 (* Fun) (A, B );

8 ......

}

Statement 1 defines a function. The input value is two integer values, and the return value is also an integer value. (The input parameter and return value can be of any other data type ); statement 3 defines a function pointer. Unlike int * or double *, the function pointer must specify the input parameter at the same time, indicating that this is a function pointer, * Fun must also be enclosed in a pair of parentheses. Statement 6 assigns a function pointer to funtion, provided that * the input parameters and return values of fun and function must be consistent. Statement 5 calls the function () directly, and Statement 7 calls the function pointer. The two are equivalent.

Of course, the advantages of function pointers cannot be seen from the above examples. The main purpose is to introduce the concept of function pointer arrays. We can see from the above example that since the function name can be saved through the function pointer, we can define an array to save several function names, which is the function pointer array. The prerequisite for correct use of the function pointer array is that these 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:

First, define 256 processing functions (and their implementations ).

Void funtion0 (void );

........

Void funtion255 (void );

Define the function pointer array and assign values to the array.

Void (* Fun [2, 256]) (void );

Fun [0] = function0;

.......

Fun [255] = function ();

Finally, the myfunction () function can be modified as follows:

Void myfuntion (char * buffer, int length)

{

_ Int8 nstreamtype = buffer [0];

(* Fun [nstreamtype]) ();

}

With only two lines of code, 256 case statements are completed, which reduces the coding workload and uses nstreamtype as the array subscript to directly call function pointers, code execution efficiency is also higher than case statements. If this is done in multiple functions, the function pointer array can better reflect its advantages.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.