------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -----
First speaking pointer function
First, the concept of pointer function
The so-called function type refers to the type of function return value, in C language allows the return value of a function is a pointer (address), such a function of the return pointer is called pointer-type function.
Second, the definition of the pointer function
The general form of defining a pointer-type function is:
int *sum (int a,int b) {
}
Type descriptor * Function name (formal parameter list) {
/* Function Body */
}
Note: The return value is a pointer.
1/* 2 pointer function: 3 4 The function of the return value is a pointer to the definition of 5 6 pointer function: 7 8 pointer type * Function name () {9 10 return address; }12 */14 #include <stdio.h>15 16//Returns the address of the large number of two digits 17//Returns the address of the large number of parameters x and y (int x, int y) {19 printf ("x =%p\n", & x); printf ("Y =%p\n", & y); return x>y?&x:& y; }24 int *max2 (int *x, int * y) {29 printf ("Max x =%p\n" , X), printf ("Max y =%p\n" , y); Return *x>*y? x:y;30 }32 int main (int argc, const char * argv[]) {33 34//Returns the address of the large number in a, b two numbers + int a=3,b=4 ; 3 6 printf ("A =%p\n", & a), Notoginseng printf ("b =%p\n", & b); int *p = MAX2 (&a,& b); %p\n ", p); 0 + + , + } 43//Prove that the parameter variable is the newly allocated space 44//argument and formal parameter only value pass /span>
Application questions:
1/* 2 think & implement 1:3 4 through the pointer function, enter an integer between 1~7 to output the corresponding week name. 5 6 */7 #include <stdio.h> 8 9 char *getday (intN) {10 11//define a string array of pointers (char *days[] ={13 14 "Monday", 15 "Tuesday" , 16 "Wednesday" , 17 "Thursday" , 18 "Friday" , 19 "Saturday" , 20 "Sunday" };23 return n>=1&&n<=7?days[n-1]: "Output Error!" ; 3 }27 int main (int argc,const char * argv[]) {printf ("%s\n", "GetDay") ("the"), and "$ 0"); +--------} ;}32 33 34/*35 Think & implement 2:36 37 The maximum value of 10 numbers is calculated by using the pointer variable as the parameter of the function. */39 #include <stdio.h>41 int getmax (int *p,int len) {th int max = * p;44 for (int i = 1;i<len;i + + ) {if (* (p+i); max) {p+ max = * ( i); }50 }51 return max;52 }53 in T Main (int argc,const char * argv[]) {a[10]={123,35,65,485,554,12,354,471,74,111 int,};56 int m = Getmax (a) , + ); printf ("M =%d\n" , m); + 0 ; span>
function pointers
First, the function pointer
in the C language, a function always occupies a contiguous memory area, and the function name is the first address of the memory that the function occupies.
We can assign this first address of the function to a pointer variable, which is the pointer variable that points to the function.
The function can then be found and called by a pointer variable. We call this pointer variable that points to a function as a "function pointer variable". (pointer variable that holds the first address of the function)
Two, function pointer definition method
The general form of a function pointer variable definition is:
Type descriptor (* pointer variable name) (parameter of function);
Type specifier denotes return value type
"(* pointer variable name )" means thatthevariable following "*" is a pointer variable defined.
The last empty parenthesis indicates that the pointer variable refers to a function.
Third, initialization of function pointers
Declaration of the 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
Initialization of function pointers
P1 = sum; Sum saves the first address of the function in memory.
int sum (int a,int b) {
return a+b;
}
int main (int argc,const char *argv[]) {
int x= 3,y = 4;
int s = SUM (x, y);
Defining function pointers
Int (*p) (int a,int b);
Initializing function pointers
p = sum; The first address of the SUM function
p = F1; No, you can't.
Note: When defining a function pointer, you do not have to write the parameter name
Int (*P1) (int, int);
P1 = sum;
return 0;
}
1/* 2 method of function pointer 3 Easy Calculator 4 */5 #include <stdio.h> 6 7//Simple calculator 8 9 int sum (int x,inty) {Ten-one return x+y;12 }14 int jian (int x,int y) {x-y;17 -}19 int cheng (int X,int y) {x*y;22 x }24 int chu (int x,int y) {[+] return x/y;27 + }29 int Main (int argc, const char * argv[]) {31 32//Define a function pointer (*p) (int, int), and a switch (2) {1: 37 p = sum;38 break; 2: p = jian;41 break, 3: p = cheng;44 break; 4 5 Case 4: the P = chu;47 break; }50//(*P) (23,45) Wuyi int s = P (23,45); printf ("%d\n", s) ; return 0;
Dark Horse Programmer-----Pointer function and function pointer