"Understanding" 10-function pointer concepts and definitions
The pointer variable holding the first address of the function is the definition of the function pointer variable function pointer variable return value type ( * variable name) (parameter of the function); Declaration of function: int sum (int A,int b);----> Function pointer int (*P1) (int a,int b); // defines a function pointer p1 // P1 can hold the return value is the int type, and there are two parameters, the type of the formal parameter is also the address of the function of type int // P1 = sum; // sum is the first address of the function in memory
#include <stdio.h>floatF1 (intXinty) { returnx+y;}intSumintAintb) { returnA +b;}intMainintargcConst Char*argv[]) { intx=3, y =5; ints =sum (x, y); //Defining function Pointers int(*p) (intAintb); //Initializing function Pointersp = sum;//the first address of the SUM function//p = F1; not possible .//Note: When defining a function pointer, you do not have to write the parameter name int(*P1) (int,int ); P1=sum; return 0;}
"Mastering" 11-function pointer use
#include <stdio.h>intSumintXinty) { returnx+y; }intJianintXinty) { returnX-y; }intChengintXinty) { returnx*y; }intChuintXinty) { returnx/y;}intMainintargcConst Char*argv[]) { //use of function pointers//1. Define function Pointers int(*p) (int,int ); //2. Initialization of function pointersp = sum;//P also points to the SUM function Switch(3) { Case 1: P=sum; Break; Case 2: P=Jian; Break; Case 3: P=Cheng; Break; Case 4: P=Chu; Break; default: Break; } //3. Calling functions indirectly with function pointers//(*p) (23,45); ints = P ( at, $); printf ("%d\n", s); return 0;}
1035
<26> "Understanding" 10-function pointer concept and definition + "mastering" 11-function pointer use +