C Language: TypeDef definition function pointer __ function

Source: Internet
Author: User
Tags assert mul
In the use of TypeDef, the most troublesome point is the pointer to a function, and without the following function, you know the definition of the expression below and how to use it.   Int (*s_calc_func (char op)) (int, int); If you do not know, please see the following program, which has a more detailed description #include <stdio.h>//Declaration four functions
int add (int, int);
int sub (int, int);
int mul (int, int);
int div (int, int); To define a pointer to this type of function
typedef int (*FP_CALC) (int, int); Int (*s_calc_func (char op)) (int, int);
Int (*s_calc_func (char op)) (int, int); and Fp_calc Calc_func (char op);
Defines a function calc_func, which returns a pointer to the corresponding calculation function, based on the action character op
Fp_calc Calc_func (char op); Returns the corresponding calculated value based on the OP
int calc (int A, int b, char op);
int add (int a, int b)
{
return a + B;
int sub (int a, int b)
{
return a-b;
int mul (int a, int b)
{
return a * b;
int div (int a, int b)
{
Return b? A/b:-1;
The purpose of this function is exactly the same as the next function job and invocation method, with the OP instead of the last two cosmetic
Int (*s_calc_func (char op)) (int, int)
{
Return Calc_func (OP);
} Fp_calc Calc_func (char op)
{
Switch (OP)
{
Case ' + ': return add;
Case '-': return sub;
Case ' * ': return mul;
Case '/': return div;

Default
return NULL;
}
return NULL;
int calc (int A, int b, char op)
{
Fp_calc fp = calc_func (OP); Here is a similar direct definition pointing to a function pointer variable

The following line is an example of a pointer to a function without a typedef, trouble.
Int (*S_FP) (int, int) = S_calc_func (OP);

ASSERT (fp = = S_FP); You can assert that these two are equal.

if (FP)
Return FP (A, b);
Else
return-1;
} void Main ()
{
int a = m, B = 20;

printf ("Calc (%d,%d,%c) =%d/n", A, B, ' + ', calc (A, B, ' + '));
printf ("Calc (%d,%d,%c) =%d/n", A, B, '-', Calc (A, B, '-'));
printf ("Calc (%d,%d,%c) =%d/n", A, B, ' * ', Calc (A, B, ' * '));
printf ("Calc (%d,%d,%c) =%d/n", A, B, '/', Calc (A, B, '/'));
Run Result: Calc (+, +) = Calc (m,-) = Calc (m, *) = Calc (100, 20,/) = 5

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.