Pointer array to function

Source: Internet
Author: User

The pointer in c is very important, but after studying c for so long, I don't know how to use the pointer array pointing to the function. In fact, it is similar to using the function pointer.

We can create an interesting structure to point to the pointer array of the function. To select a function, you only need to use the subscript of the array and then indirectly reference this pointer. This method supports the table-driven code concept. You can select the function to be executed based on the state variable (or the combination value of the state variable, instead of conditional statements or case statements. This design method is useful for adding or deleting functions from a table (or dynamically creating or changing a table.

# Include <stdio. h> void fun1 (); void fun2 (); void fun3 (); void main () {void (* fun [3]) () = {fun1, fun2, fun3}; // The Declaration of the pointer array pointing to the function int a; printf ("enter a 1, 2, or 3. enter 0 to exit. \ N "); scanf (" % d ", & a); while (a) {if (a = 1 | a = 2 | a = 3) {fun [A-1] (); // call the function printf in the array through the function pointer ("enter a 1, 2, 3. enter 0 to exit. \ n "); scanf (" % d ", & a) ;}else {printf (" enter a valid number (1-3 ). 0. Exit. \ N ") ;}} void fun1 () {printf (" function f1 is called !. \ N ");} void fun2 () {printf (" function f2 is called !. \ N ");} void fun3 () {printf (" function f3 is called !. \ N ");}

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.