Pointer knowledge sorting 7-function pointer

Source: Internet
Author: User
I. Function address: after the program runs, there is a code area in the memory. The program executes each instruction, which is read from the memory and then executed.
The so-called function address refers to the function entry address. The function starts to run from this address, that is, the command execution is obtained 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, just remember the form.
Return Value (* pointer variable name) (parameter 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){}

You can define the following pointer variable to record its address. Note that the pointer variable type must match the function type.

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

2. Obtain the function address.

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);

Test the following code:

#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 above Code, typedef and function pointer are difficult to define. Generally, we use typedef to take aliases for this type.

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);

Compared with the above two methods, the method is basically the same. When typedef is available, a new type is defined.

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 );



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.