C language: the function pointer is used to add, subtract, multiply, and divide two numbers.
//
// Main. c
// Function_pointer
//
// Created by mac on 15/8/2.
// Copyright (c) 2015 bjsxt. All rights reserved.
// Requirement: Calculate the sum, difference, product, and Quotient of two integers by using the function pointer.
// Knowledge point: a function pointer is a pointer to a function. It points to the function to be called to complete the operation.
// 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 (* p) (int, int); // defines the function pointer
P = add; // pointer to addition Function
Printf ("add = % d \ n", p (20, 10 ));
P = sub; // pointer to the subtraction Function
Printf ("sub = % d \ n", p (20, 10 ));
P = mult; // pointer to multiplication function
Printf ("mult = % d \ n", p (20, 10 ));
P = divi; // pointer to Division Function
Printf ("divi = % d \ n", p (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;
}