A series of tests of function pointers in C language

Source: Internet
Author: User

1. Start with a simple summation-quadrature function


#include <stdio.h>int Add (int a, int b) {    return a + b;} int mul (int a, int b) {    return a * b;} int main () {    int a_count = Add (5,7);    int m_count = Mul (5,7);    printf ("A_count is%d\n", a_count);    printf ("M_count is%d\n", m_count);        return 0;}




Output:


A_count is 12

M_count is 35

Program ended with exit code:0




2. Try the function pointer


#include <stdio.h>int Add (int a, int b) {    return a + b;} int mul (int a, int b) {    return a * b;} int main () {    int (*p_add) (int,int);//Declare function pointer    p_add = add;    Int (*p_mul) (int,int);    P_mul = Mul;        int a_count = P_add (5,7);    int m_count = P_mul (5,7);    printf ("A_count is%d\n", a_count);    printf ("M_count is%d\n", m_count);        return 0;}




The result is the same


A_count is 12

M_count is 35

Program ended with exit code:0




3. Simplify the definition of function pointers


#include <stdio.h>int Add (int a, int b) {    return a + b;} int mul (int a, int b) {    return a * b;} int main () {    typedef int (*fun) (int,int);//definition type fun is a pointer to a function fun    p_add = add;    Fun P_mul = Mul;        int a_count = P_add (5,7);    int m_count = P_mul (5,7);    printf ("A_count is%d\n", a_count);    printf ("M_count is%d\n", m_count);        return 0;}



The result is the same


A_count is 12

M_count is 35

Program ended with exit code:0




4. Try using a function pointer array


#include <stdio.h>int Add (int a, int b) {    return a + b;} int mul (int a, int b) {    return a * b;} int main () {    int (*func[]) (int,int) = {add,mul};//Defines the function pointer array        int a_count = func[0] (5,7);    int m_count = func[1] (5,7);    printf ("A_count is%d\n", a_count);    printf ("M_count is%d\n", m_count);        return 0;}


The result is the same

A_count is 12

M_count is 35

Program ended with exit code:0




5. Improved function pointer array


After all, after the function is more, who can remember multiplication is in the array of the first few, subscript that is how much.


#include <stdio.h>int Add (int a, int b) {    return a + b;} int mul (int a, int b) {    return a * b;} int main () {    int (*func[]) (int,int) = {add,mul};//definition function pointer array    enum func_tpye{add, mul};  The order is aligned with the above        int a_count = Func[add] (5,7);    int m_count = Func[mul] (5,7);        printf ("A_count is%d\n", a_count);    printf ("M_count is%d\n", m_count);        return 0;}


The result is the same



A_count is 12

M_count is 35

Program ended with exit code:0




6. Adjust the different functions in the function


In the previous example, directly using a function would be better with some


#include <stdio.h>typedef  Int (*fun) (int,int); int add (int a, int b) {    return a + b;} int mul (int a, int b) {    return a * b;} int Add_mul (fun f,int a,int b) {    return F (A, b);} int main () {    int (*func[]) (int,int) = {add,mul};//definition function pointer array    enum func_tpye{add, mul};  The order is aligned with the above        printf ("A_count is%d\n", Add_mul (Func[add], 5, 7));    printf ("M_count is%d\n", Add_mul (Func[mul], 5, 7));    return 0;}


The result is the same




A_count is 12

M_count is 35

Program ended with exit code:0




7. The above example can be expanded more strongly


For example, multiply the two-number multiplication to multiply the number multiplied by the sum


#include <stdio.h> #include <stdarg.h>typedef  int (*fun) (int,int), int add (int a, int b) {    return a + b ;} int mul (int a, int b) {    return a * b;} int Add_mul (fun f,int n,...) {    va_list ap;    Va_start (ap,n);    int count = Va_arg (AP, int);    for (int i = 0; i< (n-1); ++i) {        count = f (Count,va_arg (AP, int));    }    return count;} int main () {    int (*func[]) (int,int) = {add,mul};//definition function pointer array    enum func_tpye{add, mul};  The order is aligned with the above        printf ("A_count is%d\n", Add_mul (Func[add], 5,1,2,3,4,5));    printf ("A_count is%d\n", Add_mul (Func[add], 3,7,3,6));        printf ("M_count is%d\n", Add_mul (Func[mul], 5,1,2,3,4,5));    printf ("M_count is%d\n", Add_mul (Func[mul], 3,7,3,6));    return 0;}


The first parameter is a function function, the second argument is the number of operations, followed by an indeterminate parameter.

The result is:



A_count is 15

A_count is 16

M_count is 120

M_count is 126

Program ended with exit code:0






A series of tests of function pointers in C language

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.