# Pointer knowledge sorting 7-function pointer

Source: Internet
Author: User
Tags define function
I. Function address: after the program is run, there is a code area in the memory. Each Command is run, which is read from the memory and then run.
The so-called function address refers to the function entry address. The function starts to run from this address, that is, the command is run from this address.
At the code level, function addresses are stored using function pointer variables.
Ii. Basic use 1. The function pointer defines the definition of the function pointer. The syntax looks a little strange and only needs to be remembered.
Return Value (* pointer variable name) (in the form of struct type );
For example, the following four functions
`void func1(void){}int func2(void){}int func3(int a){}int func4(int a,int b){}int func5(int a,int b){}`

The following pointer variables can be defined to record their addresses. Note that the type of pointer variables must match the function type.

`void (*p1)(void);int (*p2)(void);int (*p3)(int);int (*p4)(int,int);`

The function address can be obtained in two ways: the function name or the direct function name. The two methods are equivalent.

`P1 = & func1; P2 = & func2; P3 = & func3; P4 = & func4; // The following is equivalent to p1 = func1; P2 = func2; P3 = func3; p4 = func4;`

3. You can call a function through pointer variables in either of the two ways.
(1) Replace the function name with the variable name in the same way as the function call method.
`p1();p2();p3(1);p4(1,2);`

(2) Change the function name to (* variable name) in the same way as the function call method ).
`（*p1）();（*p2）();（*p3）(1);（*p4）(1,2);`

The following code is used as an example:

`#include <stdio.h>void func1（void）{printf("this is func1\n");}void func2（void）{printf("this is func2\n");}int add(int a,int b){printf("this is add\n");return a+b;}int sub(int a,int b){printf("this is sub\n");return a-b;}int main(){void (*p1)(void);int (*p2)(int,int);int ret = 0;p1 = func1；p1();(*p1)();p1 = &func2;p1();(*p1)();p2 = add;ret = p2(1,2);printf("ret = %d\n",ret);ret = (*p2)(1,2);printf("ret = %d\n",ret);p2 = &sub;ret = p2(1,2);printf("ret = %d\n",ret);ret = (*p2)(1,2);printf("ret = %d\n",ret);return 0;}`

4. In the code above, typedef and function pointers are more difficult to define function pointers than handler. Generally, we use typedef to take aliases of such types.

`typedef  void (*type1)(void);typedef  int (*typ2)(void);typedef  int (*type3)(int);typedef  int (*type4)(int,int);void (*p1)(void);int  (*p2)(void);int (*p3)(int);int (*p4)(int,int);`

Similar to the two methods above, typedef is used to define a new type.

Type1 type2 type3 type4 is type.
P1, P2, P3, and P4 are variables.

`Type1 P1; // equivalent to void (* P1) (void); type2 P2; // equivalent to int (* P1) (void); type3 P3; // equivalent to int (* P3) (INT); type4 P4; // equivalent to int (* P4) (INT, INT );`

Related Keywords:

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.