Usage of typedef function pointers

Source: Internet
Author: User
Tags aliases arithmetic assert function definition mul

Usage of typedef function pointers

To search the function pointer on the Internet, see an example. Start not understand, want to give up, but on second thought, this usage to understand sooner or later, now spend a little more time to understand it, better than later met to spend another time to understand it. In fact, many times are so, if every time to a difficult point of content, always think about the next time I come to solve it, it will never learn things.

The following example adds a comment, which is my understanding of this usage and I hope to be helpful to the novice.

Enter the text:

 code simplification to facilitate cross-platform development.

typedef behaves a bit like a #define macro, substituting its actual type for synonymous words.

The difference: the typedef is interpreted at compile time , so the compiler has to cope with text substitution beyond the preprocessor's capabilities .

Usage One:

typedef int (*myfun) (int, int);
This usage is typically used when defining aliases for functions
The above example defines Myfun as a function pointer with a function type of two int parameter, which returns an int

In analyzing This form of definition, you can use the following method:
first remove the typedef and aliases , and the rest is the type of the original variable.
After removing the typedef and MYFUN, it is left:

Int (*) (int, int)

Usage Two:

typedef defines an alias for the variable type.

typedef struct{
int A;
int b;
}My_type;

This gives an unnamed struct an alias called My_type, so you can do this if you want to define an instance of the struct:
My_type tmp;

Second usage: typedef Primitive Variable Type aliases

Simple use of function pointers

Form 1: Return type (* function name) (parameter table)

char (*pfun) (int);

typedef Char (*pfun) (int)//with the same function as the previous line

the function of/*typedef is to define a new type . The first sentence defines a type of Ptrfun , and defines the type as a pointer to a function that takes an int as an argument and returns a char type. */

Char glfun (int a) {return;}

void Main ()

{

Pfun =glfun;

(*pfun) (2);

}

The first line defines a pointer variable pfun. It is a pointer to a function, which is an int type and the return value is a char type. only the first sentence we can not use this pointer, because we have not yet assigned to it .

The second line defines a function glfun (). The function is exactly a function that returns char as an int parameter. We are going to understand the function-function's function name from the pointer level is actually a pointer , the function name points to the code in memory of the first address .

Here's an example: C code//#include <iostream.h> #include <stdio.h>typedef int(*FP_CALC) (int,int); Note that this is not a function declaration but a function definition, it is an address, you can directly output add to seeintAddintAintb) {returnA + b; }intSubintAintb) {returnA-B; }intMulintAintb) {returnA * b; }intDivintAintb) {returnB?   A/b:-1; }//define a function with the parameter op and return a pointer. The pointer type is a function pointer that has two int parameters,///return type int. Its function is to return the address of the corresponding function according to the operator Fp_calc Calc_func (CharOP) {Switch(OP) { Case'+':returnadd;//returns the address of the function Case'-':returnSub Case'*':returnMul Case'/':returnDivdefault:returnNULL; }returnNULL; }//s_calc_func is a function whose argument is OP,//return value is a function pointer with two int parameter, return type intint(*s_calc_func (CharOP)) (int,int)   {returnCalc_func (OP); }//The function that the end user calls directly, the function receives two int integer, and an arithmetic operator, returns two number of operation resultintCalcintAintBCharOP) {Fp_calc FP = Calc_func (OP);//Address of a function that obtains various operations according to the budget symbolint(*S_FP) (int,int= S_calc_func (OP);//For Test//ASSERT (fp = = S_FP); Can assert that the two are equal.if(FP)returnFP (A, B);//The corresponding function is called according to the address of the function obtained in the previous step, and the result is returnedElse return-1; }voidMain () {int A = +, 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, '-')); &NBSP;&N Bsp       printf ("Calc (%d,%d,%c) =%d\n", A, B, ' * ', Calc (A, B, ' * '));     &nbs p;   printf ("Calc (%d,%d,%c) =%d\n", A, B, '/', Calc (A, B, '/'));  } 

#include <iostream.h> #include <stdio.h> typedef int (*FP_CALC) (int, int);  Note that this is not a function declaration but a function definition, it is an address, you can directly output add to see int add (int a, int b) {return a + B;} int sub (int a, int b) {return a-A;} int Mul (int A, int b) {return a * b;} int div (int a, int b) {return B? A/b:-1;}//define a function, the parameter is OP, return a pointer. The pointer type is a function pointer that has two int parameters,///return type int. Its function is to return the address of the corresponding function according to the operator Fp_calc Calc_func (char op) {switch (OP) {case ' + ': Return add;//Returns the address of the function case '-': return sub;
* ': Return mul;
Case '/': return div;
Default:return NULL;
} return NULL; }//s_calc_func is a function whose parameters are OP,//return value is a function pointer int (*s_calc_func (char op)) with two int parameter, return type int (int, int) {return Calc_func (o
p); }//The function that the end user calls directly, the function receives two int integer, and an arithmetic operator, returns two number of operation result int calc (int A, int b, char op) {Fp_calc FP = Calc_func (OP);//According to the budget   The address of the calculated function int (*S_FP) (int, int) = S_calc_func (OP);//For Test//ASSERT (fp = = S_FP);
It can be asserted that these two are equal if (FP) return FP (A, B);//The corresponding function is called according to the address of the function obtained in the previous step, and returns the result else return-1; } void Main () {int 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, ' * '));
printf ("Calc (%d,%d,%c) =%d\n", A, B, '/', Calc (A, B, '/')); }

Run results

Calc (100, 20, +) = 120

Calc (100, 20,-) = 80

Calc (100, 20, *) = 2000

Calc (100, 20,/) = 5 for your safety, please only open URLs with reliable sources

Open site cancellation from: http://hi.baidu.com/%D6%EC%CF%E9/blog/item/482290cb4b6dfeed53664fda.html

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.