The 36th lesson function and pointer analysis

Source: Internet
Author: User

1. function Type

(1) A function in C has its own particular type , which is determined by the return value , the parameter type , and the number of parameters . such as int add (int i,int j) is of type int (int,int).

(2) renaming a function type by typedef in C language

typedef Type name(parameter list);//as TypeDef int F (INT,INT);

2. Function pointers

(1) A function pointer is used to point to a function, and the function name is the entry address of the execution body.

(2) Two methods for defining function pointers

① is defined by the function type: functype* pointer;

② Direct definition:type(*pointer) (parameter list);

Where type is the return value type , pointer is the function pointer variable name, parameter list is the parameter type

Use of the "instance analysis" function pointer (tip: Jump directly to a fixed address using a function pointer to start execution)

#include <stdio.h>typedefint(FUNC) (int);intTestinti) {    returnIi;}voidf () {printf ("Call f () ... \ n");}intMain () {FUNC* pt = test;//legal, the function name is the entry address of the function body.//directly defines the function pointer, &f is the old-fashioned notation. The function name is just a symbol (not a variable),//As with the array name, no memory is allocated for it, so &f and F are equal in value.     void(*PF) () = &F; //If you know the address of a function, this can be changed to a fixed address value, to achieve jump!  printf ("PF =%p\n", PF); printf ("f =%p\n", F); printf ("&f =%p\n", &f);//The result should be: PF = = f = &f;pf ();//call with a function pointer        (*PF) ();//old-fashioned notationprintf ("Function Pointer call:%d\n", PT (2)); return 0;}

3. Callback function

(1) callback function is a calling mechanism implemented by function pointers

(2) Principle of callback mechanism

① caller does not know the specific function to invoke when a specific event occurs

The ② function does not know when it is called, only the tasks that need to be done

③ when a specific event occurs, the caller invokes the specific function through the function pointer .

(3) The caller and callee in the callback mechanism are not dependent on each other.

Instance analysis callback function using example

#include <stdio.h>typedefint(*weapon) (int);//the function of manipulating a weapon//fight with a boss using some kind of weaponvoidFight (weapon WP,intArg//ARG is the parameter passed to the function pointer{    intresult =0; printf ("Fight boss!\n"); Result= WP (ARG);//call the callback function and pass in the parameter argprintf ("Boss loss:%d\n", result);//How much does boss bleed? }//using weapons--knivesintKnife (intN) {    intRET =0; inti =0;  for(i=0; i< N; i++) {printf ("Knife attack:%d\n",1); RET++; } printf ("\ n"); returnret; }//using weapons-swordsintSwordintN) {    intRET =0; inti =0;  for(i=0; i< N; i++) {printf ("Sword attack:%d\n",5); RET++; } printf ("\ n"); returnret; }//using weapons--gunsintGunintN) {    intRET =0; inti =0;  for(i=0; i< N; i++) {printf ("Gun attack:%d\n",Ten); RET++; } printf ("\ n"); returnret; }intMain () {Fight (knife,3);//cut it 3 times with a knife .Fight (Sword,4);//Stab 4 times with a sword .Fight (Gun,5);//shot 5 times .    return 0;}

4. Summary

(1) There are specific types of functions in the C language

(2) Function pointers can be defined using function types

(3) Function pointer is the key technique to implement callback mechanism

(4) Through the function pointer can be implemented in C program fixed address jump

The 36th lesson function and pointer analysis

Related Article

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.