C Language Study Notes
The pointer to the function and the function that returns the pointer value.
The pointer is a variable that stores the address. This variable points to the actual value. The pointer not only points to the variable, but also to the function.
I. pointer to the function 1. pointer to the Function
During compilation, a function is assigned an entry function, that is, the first address. This entry address is the function pointer. Just as the array name acts as the first address of the array, the C language requires that the function name also acts as the first address of the function. This first address is given to a specific pointer variable, which points to this function and can be used to call the function.
The implementation of this process can generally be divided into three steps:
1. Define the pointer variable pointing to the Function 2. Point pointer variables to a function. 3. Call a function using the pointer variable of the pointer Function |
Let's use an example: enter 10 numbers to calculate the maximum value)
Let's take a look at the general function call method.
# Include <stdio. h> int max (int * p); // declare the function main () {int I, m, a [10]; printf ("Enter 10 numbers \ n "); for (I = 0; I <10; I ++) scanf ("% d", & a [I]); m = max (a); // call a function, a is the first address of the array, that is, a [0] printf ("max = % d \ n", m);} int max (int * p) // function. * p defines the pointer Variable p, which has no practical significance. In this case, p = a; {int I, t = * p; for (I = 1; I <10; I ++) {if (* (p + I)> t) t = * (p + I) ;} return t ;}
650) this. width = 650; "title =" image 1.png "src =" http://www.bkjia.com/uploads/allimg/131228/2112043415-0.png "orgsrc =" http://www.bkjia.com/uploads/allimg/131228/2112043415-0.png "/> |
After understanding the entire process, let's use the method to call the pointer to the function:
# Include <stdio. h> int max (int * p); // declare the function main () {int I, m, a [10], max; int (* f )(); // define the pointer variable f printf to the function ("Enter 10 numbers \ n"); for (I = 0; I <10; I ++) scanf ("% d", & a [I]); f = max; // pointer variable pointing to function max m = (* f) (a); // call function, (* f) represents the function max printf ("max = % d \ n", m);} int max (int * p) // function, * p defines the pointer Variable p, which has no practical significance. In this case, p = a; {int I, t = * p; for (I = 1; I <10; I ++) {if (* (p + I)> t) t = * (p + I);} return t ;}
650) this. width = 650; "title =" image 2.png "src =" http://www.bkjia.com/uploads/allimg/131228/21120414L-2.png "orgsrc =" http://www.bkjia.com/uploads/allimg/131228/21120414L-2.png "/> |
We can see that the running results of the two programs are the same.
2. pointer to function as function parameter
When a pointer to a function is used as a function parameter, the first address of a function is passed to the called function during function calling, so that the passed function is called in the called function. For example:
Main function p1 = max; // function p1, with two parameters p2 = min; // function p2, with two parameters .... inv (p1, p2); // name the function as the first address to the called function DemoInv called function/* accept the function parameters, define the pointers x1 and x2, and pass p1 to x1, p2 is passed to x2 */DemoInv (int (* x1) (int, int), (* x2) (int, int )){... y1 = (* x1) (a, B); // equivalent to the calculation function p1 y2 = (* x2) (a, B );}
Program instance
# Include <stdio. h>/* function declaration */int max (int x, int y); process (int x, int y, int (* fun) (int, int )); main () {/* function as the declaration of real parameters */int min (int, int); int max (int, int); int add (int, int); int, b; printf ("enter 2 numbers \ n"); scanf ("% d, % d", & a, & B ); /* pass the function as a real parameter */printf ("min = "); /* pass a and B to x and y in the worker function process, and then pass the function min as the address to fun */process (a, B, min ); printf ("max ="); process (a, B, max); printf ("add ="); process (a, B, add );} /* calculate the minimum value */int min (int x, Int y) {int z; z = (x <y )? X: y); return z;}/* calculate the maximum value */int max (int x, int y) {int z; z = (x> y )? X: y); return z;}/* calculate the sum of the two numbers */int add (int x, int y) {int z; z = x + y; return z;}/* Called function. At this time, fun points to various functions max, min, add) */process (int x, int y, int (* fun) (int, int) {int result; result = (* fun) (x, y); // equivalent to executing max (x, y), min (x, y ), add (x, y); printf ("% d \ n", result );}
650) this. width = 650; "title =" image 3.png "src =" http://img1.51cto.com/attachment/201308/135549654.png "orgsrc =" http://img1.51cto.com/attachment/201308/135549654.png "/> |
2. functions that return pointer values
If a pointer can point to a function, the function can also return the return value of the pointer type.
The general format is:
Type name * function name parameter) for example: int * fun (int x, int y)
Program instance
# Include <stdio. h>/* function declaration */int * fun (int x, int y); main () {int * fun (int, int ); // For p = fun (I, j) declaration function fun int * p, I, j; printf ("Please input two numbers: \ n "); scanf ("% d, % d", & I, & j); p = fun (I, j); // give the variable address of function fun to the pointer Variable p, printf ("max = % d \ n", * p); // output value}/* function fun calculates the address * fun is equivalent to defining a pointer variable, here fun is a function, that is, the pointer function */int * fun (int x, int y) {int * z; if (x> y) z = & x; // return address else z = & y; return z ;}
650) this. width = 650; "title =" image 4.png "src =" http://www.bkjia.com/uploads/allimg/131228/2112043134-6.png "orgsrc =" http://www.bkjia.com/uploads/allimg/131228/2112043134-6.png "/> |
This article is from the "Zhao Yuqiang's blog" and is not reposted!