Initial look function pointer

Source: Internet
Author: User

We usually know that a pointer is used to store the address of a variable. This variable has the same data type as the pointer type. In addition to this function, the pointer can also point to the address of a function to call the function to be pointed.

(1) function pointer declaration:

Int (* pfun) (double, int );

In this way, a function pointer pointing to the parameter type of double and int is declared.


Function pointers are generally composed of three parts:

1. Point to the return type of the Function

2. pointer name

3. Type of the parameter pointing to the Function


(2) how to point to a function:

Long sum (long a, long B );

Long (* pfun) (long, long) = sum;

In this way, the * pfun function pointer is used to point to the sum function.


Simple Example:

[Cpp]
# Include <iostream>
 
Using std: cout;
Using std: endl;
 
Long sum (long a, long B );
Long product (long a, long B );
 
Int main ()
{
Long (* pdo_it) (long, long );
Pdo_it = product;
Cout <endl <"3*5 =" <pdo_it (3, 5 );
 
Pdo_it = sum;
Cout <endl <"3 * (4*5) + 6 =" <pdo_it (product (3, pdo_it (4, 5), 6 );
Cout <endl;
 
System ("pause ");
Return 0;
}
 
Long product (long a, long B)
{
Return a * B;
}
 
Long sum (long a, long B)
{
Return a + B;
}
The result is:

 

(3) function pointer as real parameter:
Because "pointer to function" is a completely reasonable type, a function can have a form parameter of the type as a function pointer. Then, such a function calls the function pointed to by the real parameter.

Example:

[Cpp]
# Include <iostream>
 
Using std: cout;
Using std: endl;
 
Double squared (double );
Double cubed (double );
Double sumarray (double array [], int len, double (* pfun) (double ));
 
Int main ()
{
Double array [] = {1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5 };
Int len (sizeof array/sizeof array [0]);
 
Cout <endl <"Sum of Squared =" <sumarray (array, len, squared );
Cout <endl <"Sum of cubed =" <sumarray (array, len, cubed );
Cout <endl;
 
System ("pause ");
Return 0;
}
 
Double squared (double x)
{
Return x * x;
}
 
Double cubed (double x)
{
Return x * x;
}
 
Double sumarray (double array [], int len, double (* pfun) (double ))
{
Double total (0.0 );
For (int I = 0; I <len; ++ I)
{
Total + = pfun (array [I]);
}
Return total;
}
The result is:


(4) function pointer array:

Assume there are three functions:

Double add (double, double );

Double mul (double, double );

Double div (double, double );

Double (* pfun [3]) (double, double) = {add, mul, div };

You can call:

Pfun [1] (1.5, 5.5 );

 

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.