The magical functions of function pointers and array of function pointers

Source: Internet
Author: User
Tags case statement

I encountered such a problem in the development of a software process, the front-level module passed to my binary data, the input parameters of char* buffer and int length,buffer is the first address of the data, length represents the size of this batch of data. The data is characterized by a variable length, a variable type, and a 256 (28) Probability of identifying the type of the data by the first byte (Buffer[0]). My task is to have to deal with every possible data type, and my module contains several functions that are handled in a similar way within each function. If you follow the usual practice, you will write the following code:

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 down this way, you have to make so many judgments in every one of my functions, the code must be very long, and every time you deal with it, Have to make many judgments before finding the correct processing function, code execution efficiency is not high. In response to the above problem, I think of the method of using a function pointer array to solve this problem. The concept of the

function pointer, as mentioned in the classic tutorial of Mr. Tam Hao's C programming, is not used in most cases and ignores its existence. The function name is actually a pointer to the entry address of the function, but it is different from the normal, such as int*, double* pointers, see the following example to understand the concept of function pointers:
int funtion (int x, int y);
void main (void)  
{
  int (*fun) (int x, int y);
  int a = ten, B = 20;
  Function (A, b);
  fun = function;
  (*fun) (A, b);
  ...
}
Statement 1 defines a functional function whose input is two integers and returns an integer number (input parameter and return value can be any other data type); Statement 3 defines a function pointer, which differs from the int* or double* definition pointer. The definition of a function pointer must also indicate the input parameter, indicating that it is a function pointer, and that *fun must be enclosed in parentheses, and statement 6 assigns a function pointer to funtion, provided that the input and return values of *fun and functions must be consistent. Statement 5 calls function functions directly (), and statement 7 is called function pointers, which are equivalent.

Of course, from the above example does not see the advantages of function pointers, the purpose is to elicit the concept of function pointer array. We can tell from the above example that since function names can be saved by function pointers, it is also possible to define an array to hold several function names, which is the array of function pointers. The proper use of a function pointer array is that several functions that need to be saved through a 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:

256 processing functions (and their implementations) are defined first.

void Funtion0 (void);
........
void funtion255 (void);

Next, define the array of function pointers and assign values to the array.
void (*fun[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]) ();
}

As long as 2 lines of code, the completion of 256 case statements to do, reduce the amount of work to write code, the Nstreamtype as an array subscript, directly call the function pointer, from the code execution efficiency, but also higher than the case statement. If you want to do this in more than one function, the function pointer array will show its advantages.

Reprinted: 74295692

The magical functions of function pointers and array of function pointers

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.