Function pointer

xiaoxiao2021-03-06  41

In the program run, the global variable is stored in the DATA area, and the local variable is placed in the stack area, and the dynamic variable is placed in the pile area. The function code is the algorithm instruction section of the program, and also occupies the memory space, stored in the Code area.

Each function has an address. Pointer to the function address is called a function pointer.

The function pointer points to a function in the code area, and the corresponding function can be called through the function pointer.

1. Define function pointer:

INT (* func) (Char A, Char B);

INT is the return type of the function; * indicates that the back FUNC is a pointer variable name; the later () indicates that the definition is a function or function pointer, which is a function pointer. This function has two character types of parameters a and b.

After defining a function pointer, a pointer variable is generated, which has a global, static, and partial points. Have a memory space.

2. The intrinsic difference of the function pointer! ! The array name of [] is omitted, omitted () functions are also addresses, so the () function name can be omitted as a function pointer. (But pay attention to the parameters, return the problem!)

note! ! The pointers for function pointers and other data types are all addresses, but the type is very different. They are not allowed to assign each other! Even, the explicit conversion is not! Because: The function pointer points to the Code area of ​​the program, is the command code running, and the data pointer points to the Data, Stack and Heap District, which is the various data that is running to run.

3. Call the function through the function pointer: First, you must assign the function to the function! Such as: INT FN3 (INT A); fp2 = fn3; // OK, function parameters and return value types, function names are assigned to function pointers!

Then: fp2 (5); or (* fp2) (5);

4. Simplify the pointer with TypedEf: Sometimes, you often define the same function pointer, it is best to declare this function pointer: typedef int (* fun) (Int A, int b); // Declaration Fun is a function pointer type Fun funp; // funp is a function pointer to a return intellectual and two shaping parameters! Fun is a function pointer type, the pointer in the pointer type points to a function, it has two shaping parameters, returns an integer value. FUN is not a pointer variable, just a pointer type name. Fun funp; function pointer definition determines a function pointer variable FUNP.

5. Function pointer constitutes the argument:

#include

Typedef void (); void f1 () {cout << "good! / n";} void f2 () {cout << "better! / n";} void f3 () {cout << " Best! / n ";

Menufun fun [] = {f1, f2, f3};

Void main () {INT Choice; do {cout << "1 ------- Display good / n" << "2 -------- Display better / n" << "3- ------- Display best / n "<<" 0 -------- exit / n "<<" Enter Your Choice: "; cin >> choice; switch (kice) {cas 1: Fun; Case 2: FUN [1] (); Break; Case 3: Fun [2] (); Break; Case 0: Return; Default: cout << "you entered A Wrong Key. / N ";}}. The return type of the function can be a function pointer: for example: typedef int (* sig) (); // Declare to return an integer and unparalleled function pointer type SIG TYPEDEF VOID (* SigarG) (); // Non-return and no Function Pointer Type SIG SIGNAL (INT, SIGARG); // Defines a function of returns a function pointer

SIG Signal (int, sigarg) Defines a function that returns a function pointer and has a shaping parameter and a function pointer parameter. It returns the function pointer to the return to the integer and no parameters

转载请注明原文地址:https://www.9cbs.com/read-56290.html

New Post(0)