C/C ++ learning: function pointer, learning function pointer

Source: Internet
Author: User

C/C ++ learning: function pointer, learning function pointer

In the past, I did not pay much attention to pointer-related functions, but I recently used them in practical work. So I learned it carefully.

Function pointer Declaration

It is easy to declare a function pointer, that is, to replace the function name in the function declaration with a pointer:

C/C ++Int test (int para1, double * para2); // function declaration int (* pf) (int para1, double * para2); // function pointer Declaration

NOTE: You must enclose*pfEnclose, because the Priority Ratio of parentheses*The operator is high, so:

C/C ++Int (* pf) (int para1, double * para2); // a pointer to the function int * pf (int para1, double * para2); // a function that returns the pointer

After declaring the function pointer, the next step is to assign a value to the function pointer, that is, point the function pointer to a type-matching function (which is consistent with the pointer of the basic type ),The name of a function is the address of the function.:

C/C ++Pf = test; int (* pf1) (int para1, double * para2) = test; // initialization can also be completed when the function pointer declares

C ++ 11 has the automatic type inference function, which is much simpler:

C++11 codeauto pf = test;
Call a function using a function pointer

(*pf)Assume the same role as the function name, so use(*pf)You only need to think of it as the function name:

C/C ++Int n = 0; double d = 0.0; int r = 0; r = (* pf) (n, & d); r = pf (n, & d ); // This method is also allowed for C/C ++
Function pointer Array

We may also need to use a function pointer array, for example:

C/C ++Int test1 (int para1, double * para2); // function declaration int test2 (int para1, double * para2); // function declaration int test3 (int para1, double * para2); // function declaration int (* pfArray [3]) (int para1, double * para2) = {test1, test2, test3 }; // function pointer array declaration and initialization r = * pfArray [0] (n, & d); // function call

It can be seen that using function pointers in this way is troublesome. Imagine if the above function returns a const pointer and we want to declare the array of function pointers as unchangeable, then thisconstWhere should I add it? Here we provide another simple solution, that is, to simplify the process by using typedef.

Simplify with typedef
C/C ++Typedef int (* p_fun) (int para1, double * para2); // this way, we can use the function pointer like a common type p_fun pf = test; const p_fun pf1 = test; // constant function pointer declaration and initialization. Note that it is the same as the common const data type, constant function pointer must complete initialization during declaration p_fun pfArray [3] = {test1, test2, test3}; const p_fun pfArray1 [3] = {test1, test2, test3 }; // constant function pointer array declaration and initialization

It's that easy!

Reference books: C ++ Primer Plus (6th) Chinese edition


Explain the functions of C function pointers

The above paragraph is really a pitfall for beginners. What official words are like, so I can only understand fuzzy details.
Let me give you an example of writing a function pointer.
Int * p;
Int;
P = & a; // This is a general integer pointer
# Include <iostream>
Using namespace std;
Int arr (int a, int B) {return 0;} // For example, function 1
Int brr (int a, int B) {return 1;} // For example, function 2.
Int main ()
{
Int (* frr) (int, int); // here is the function pointer
Frr = arr; // it can point to arr
Cout <frr (1, 2) <endl;
Frr = brr; // You can also point to brr. You only need to change the object to which you want to operate different functions.
Cout <frr (1, 2) <endl;
Return 0;
}

How to Use the return function pointer in C Language

Can I help you with the C language learning materials?
You can log on to view details.
Hope to be useful to you

Remember to adopt it

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.