Pointer to a function

Source: Internet
Author: User

If a function is defined in a program, the compilation system allocates a bucket for the function code during compilation. The starting address (also called the entry address) of the bucket is the pointer to the function. You can define a pointer variable pointing to a function to store the starting address of a function. This means that the pointer variable points to the function. For example, INT (* p) (INT, INT) indicates that p is a pointer variable pointing to a function. It can point to a function of an integer type and has two integer parameters, the p type is int (*) (INT, INT ).

One important purpose of pointer variables pointing to functions is to pass the function address as a parameter to other functions, a bit like a set of interfaces. In many applications, a menu prompt is often used to enter a number, and then different functions are called based on different input values to implement different functions. You can also use the if or switch statement to call different functions without using pointer variables. However, it is clear that the use of pointer variables makes the program more concise and professional.

The basic concepts are omitted. Here I want to explain some usage through examples, here is an example program compiled by myself that uses a pointer to a function (compiled in Visual C ++ 6.0 ).

/** Use a pointer to a function to test multiple function points * Time: 2.10.13 * Author: Jiang Wu * version: 1.0 */# include <stdio. h> # include <math. h> # define DETA 0.00001 // The interval between each small step credits. The smaller the DETA error, the smaller the int main () {// function declaration double F1 (double, double ); // The integral function y = x + 1; double F2 (double, double); // The integral function y = 2 * x + 3; double F3 (double, double ); // integral function y = exp (x) + 1; double F4 (double, double); // integral function y = (x + 1) ^ 2; double F5 (double, double); // The integral function y = x ^ 3; Double Integral (double A, do Uble B, double (* Fun) (double, double); // call the function interface uniformly and use the int n pointer to the function; // indicates the input function's choice of Double A, B; // A, B is the upper and lower limits of function points while (1) {printf ("========================================== =======\ N "); printf ("function to be solved: (1 ~ 5 is selected as follows, 0 is reselected,-1 exits :) \ n "); printf (" 1.y = x + 1; \ n "); printf ("2.y = 2 * x + 1; \ n"); printf ("3.y = exp (x) + 1; \ n "); printf ("4.y = (x + 1) ^ 2; \ n"); printf ("5.y = x ^ 3; \ n "); printf ("the integral function you require is:"); scanf ("% d", & N); If (n = 0) continue; // when n = 0, reselect the function if (n =-1) break; // when n =-1, jump out of the loop printf ("============================ =====================\ N "); printf ("Enter the upper limit and lower limit for points:"); scanf ("% lf", & A, & B ); printf ("========================================== =====\ N "); when switch (n) {Case 1: // n = 1, call F1 to obtain the credits, the same as printf ("credits: % 7.2lf \ n", integral (A, B, f1); break; Case 2: printf ("points: % 7.2lf \ n", integral (a, B, F2); break; Case 3: printf ("points: % 7.2lf \ n", integral (a, B, F3); break; Case 4: printf ("points: % 7.2lf \ n ", integral (a, B, F4); break; Case 5: printf ("points: % 7.2lf \ n", integral (a, B, F5); break; default: break;} return 0;} Double Integral (double A, double B, double (* Fun) (double, double) {double result; Result = fun (, b); return result;} double F1 (double A, double B) {Double X = A, Y; double area = 0; double temp; // B> A normal calculation, A <B is used to exchange the values of A and B. The calculated points are the opposite. F2, F3, F4, and F5 are the same. If (B>) {for (x = A; x <= B; X = x + DETA) {Y = x + 1; Area = Area + DETA * Y;} return (area );} else {temp = A; A = B; B = temp; For (x = A; x <= B; X = x + DETA) {Y = x + 1; area = Area + DETA * Y;} return (-area) ;}} double F2 (double A, double B) {Double X = A, Y; double area = 0; for (x = A; x <= B; X = x + DETA) {Y = 2 * x + 3; Area = Area + DETA * Y;} return area ;} double F3 (double A, double B) {Double X = A, Y; double area = 0; double temp; If (B> A) {for (x =; x <= B; X = x + DETA) {Y = exp (x) + 1; Area = Area + DETA * Y;} return area;} else {temp =; A = B; B = temp; For (x = A; x <= B; X = x + DETA) {Y = exp (x) + 1; area = Area + DETA * Y;} return (-area) ;}} double F4 (double A, double B) {Double X = A, Y; double area = 0; double temp; If (B> A) {for (x = A; x <= B; X = x + DETA) {Y = (x + 1) * (x + 1); Area = Area + DETA * Y;} return area;} else {temp = A; A = B; B = temp; For (x =; x <= B; X = x + DETA) {Y = (x + 1) * (x + 1); Area = Area + DETA * Y ;} return (-area) ;}} double F5 (double A, double B) {Double X = A, Y; double area = 0; For (x =; x <= B; X = x + DETA) {Y = x * X; Area = Area + DETA * Y;} return area ;}

Every time I use a pointer to a function, I think it is a little redundant. In fact, the switch and if functions can be well completed, and the switch is also used in this example. My point is to make myself look better. If you understand the obvious advantages of using pointers to functions, please let me know.

In addition, I want to raise a question, so far I have not found the answer. See the following program:

# Include <stdio. h> int main () {int max (INT, INT); // function declaration int (* p) (INT, INT ); // define the pointer variable pint a, B, c; P = max; // P points to the max function printf ("Please enter A and B :"); scanf ("% d", & A, & B); C = (* p) (A, B ); // call the max function printf ("A = % d \ NB = % d \ Nmax = % d \ n", a, B, c) through pointer variables; return 0 ;} int max (int x, int y) {int Z; return z = x> Y? X: Y ;}

In the program, c = (* p) (a, B) is used to call the max function equivalent. in Visual C ++ 6.0, the code is compiled and the correct result is obtained.

However, since P = max is used to store the entry address of the max () function, (* P) is the value of the address, rather than the address, so the program compilation fails. Instead, use C = P (A, B) to call the max () function. Coincidentally, the call can also get the correct result. That is to say, c = (* p) (a, B) and c = P (A, B) are equivalent (at least for me ). Just as my own program used the latter. The current capabilities are limited. If you know why, I am honored to discuss it with you!

In addition, when using pointers to functions, the corresponding data types should be the same, as shown in the first two examples. This makes sense. For example, in the second example, int max (INT, INT) and INT (* p) (INT, INT) are of the same type as the return values.

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.