Usage of function pointers in C

Source: Internet
Author: User

Usage of function pointers in C

After understanding the description and pointer in C, it is easy to look at the usage of function pointers.

In C, function pointers are mainly used for two purposes:

1. Pass it as a parameter to another function (that is, as a callback function)

2. conversion table (jump table)

 

The following describes the usage of callback functions and conversion tables respectively.

1. Callback Function

Make the search function irrelevant to the type in the search of the linked list

 

/* Find a function with a specified value in a single-chain table. Its parameter is a pointer to the first node of the linked list * a pointer to the value we need to find and a function pointer, the function it points to is used to compare the type values stored in the linked list */# include
 
  
# Include
  
   
Node * search_list (Node * node, void const * value, int (* compare) (void const *, void const *) {while (node! = NULL) {if (compare (& node-> value, value) = 0) break; node = node-> link;} return node ;}
  
 

Then write the comparison function:

 

 

int compare_ints(void const *a,void const *b){if(*(int *)a==*(int *)b)return 0;elsereturn 1;}

Finally, you can call it as follows:

 

desired_node=search_list(root,&desired_value,compare_ints);

2. Transfer table

 

Two steps to create a transfer table: first, declare and initialize a function pointer array. The only thing to note is that the prototype of this function appears before the declaration of this array.

Double add (double, double );

Double sub (double, double)

...

Double (* oper_func []) (double, double );

The second step is to replace the switch statement with oper_func [delimiter] (op1, op2.

The following is a program that reads some characters from the standard input and calculates the percentage of all types of characters based on the following categories:

Control Character iscntrl

Space

Digital isdigit

Lower case letter islower

Uppercase letter: isupper

Punctuation ispunct

The printable character isprint is a printable character.

The above character categories all have corresponding functions in ctype. h.

Use the following method to convert a table without using the if statement:

 

# Include
 
  
# Include
  
   
# Include
   
    
/* Calculate the percentage of characters accumulated from the standard input * // * define a function to determine whether a character is printable. This can eliminate * Special cases */int is_not_print (int ch) {return! Isprint (ch);}/* used to differentiate the jump table of each type of classification function */static int (* test_func []) (int) = {iscntrl, isspace, isdigit, islower, isupper, ispunct, is_not_print}; # define N_CATEGORIES (sizeof (test_func)/sizeof (test_func [0]) /* Name of each character */char * label [] = {control, whitespace, digit, lower case, upper case, punctuation, non-printable }; /* The number of characters and total number of characters of each type currently seen */int count [N_CATEGORIES]; int total; int main () {int ch; int category; /* read and process each character */while (ch = Getchar ())! = EOF) {total + = 1;/* Call each test function for this character. If the result is true, increase the value of the counter */for (category = 0; category
    
     
We can clearly see the benefits of using a conversion table. The code is much shorter and clearer.
     

 

The test result is as follows:

 

 

Add a small knowledge point, that is, EOF. enter a new line under the command line, press ctrl + z, and then press enter, that is, EOF

 

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.