C language: the function pointer is used to add, subtract, multiply, and divide two numbers (the function pointer is used as a parameter ).
//
// Main. c
// Function_pointer
//
// Created by mac on 15/8/2.
// Copyright (c) 2015 bjsxt. All rights reserved.
// Requirement: Use the function pointer as a parameter to calculate the sum, difference, product, and Quotient of two integers.
// Knowledge point: a function pointer is a pointer to a function. It points to the function to be called to complete the operation. In fact, this pointer is the entry address pointing to the function.
// Remember: the function to be called must be the same as the declared function pointer (including the return value type, number of parameters, and type)
# Include <stdio. h>
Int add (int, int );
Int sub (int, int );
Int mult (int, int );
Int divi (int, int );
Int main (int argc, const char * argv [])
{
Int function (int (* p) (int, int); // set the function pointer as a parameter.
// P is a pointer variable pointing to a function. It can point to a function with an integer type and two integer parameters. The p type is represented by int (*) (int, int ).
Printf ("add = % d \ n", add (20, 10 ));
Printf ("sub = % d \ n", sub (20, 10 ));
Printf ("mult = % d \ n", mult (20, 10 ));
Printf ("divi = % d \ n", divi (20, 10 ));
Return 0;
}
Int add (int a, int B)
{
Return a + B;
}
Int sub (int a, int B)
{
Return a-B;
}
Int mult (int a, int B)
{
Return a * B;
}
Int divi (int a, int B)
{
Return a/B;
}