[C ++] functions, pointers, and function pointers
Recently, I have been reading C ++ primer plus. I feel that there are many difficulties in the chapter of functions and pointers. I want to take notes and enhance my understanding.
From C ++ Primer Plus: Chapter 7 Function: C ++ Programming Modules
1. How to declare a function pointer?
Similar to the function prototype: You need to declare the pointer to the function's return value and parameter list.
Double pam (int); // The parameter is of the int type, and the return value is of the double type. The double (* pf) function; (int) // point to the int type parameter, the pointer pf = pam whose return value is double type; // The function name represents the function address double x = pam (4); // The function name calls double x = (* pf) (4); // the pointer calls double x = pf (4); // C ++ also allows the pointer name to be used as the function name
2. C ++ 11 automatic type inference
const double * f1(const double *, int);const double * (*p1)(const double *, int); //p1 poitns to f1auto p2 = f1; //C++11 automatic type deduction,p2 points to f1 as well
3. Use the pointer name as the function name
// The preceding function is of the double * type. The first part of cout returns the double pointer, and the second part returns the value cout pointed to by the double pointer <(* p1) (av, 3) <": "<* (* p1) (av, 3) <endl; // same as the preceding cout, it only uses the function pointer name to call the function cout <p2 (av, 3) <":" <* p2 (av, 3) <endl;
4. function pointer Array
Const double * (* pa [3]) (const double *, int) = {f1, f2, f3}; // create a function pointer array // call a function through a pointer, returns the pointer const double * px = pa [0] (av, 3 ); // call by pointer as if it were a function nameconst double * py = (* pa [0]) (av, 3 ); // normal call // obtain the value pointed to by the pointer returned by the function. double x = * pa [0] (av, 3); double x = * (* pa [0]) (av, 3 );
5. pointer to the pointer Array
Differences between pointer arrays and array pointers
*pd[3] //an array of 3 pointers(*pd)[3] //a pointer to an array of three elements
Pointer to array
1 auto pc = & pa; // & pa is the address of the entire array, and pa is the first address of the array element 2 3 const double * (* pd) [3]) (const double *, int) = & pa; // equivalent to the first 4 5 ** & pa = * pa = pa [0]
Code:
1 //arfupt.cpp -- an array of function pointers 2 #include<iostream> 3 //various notations,same signatures 4 const double *f1(const double ar[],int n); 5 const double *f2(const double [],int); 6 const double *f3(const double *,int); 7 8 int main() 9 {10 using namespace std;11 double av[3] = {1112.3,1542.6,2227.9};12 13 //pointer to a function14 15 const double *(*p1)(const double *,int) = f1;16 auto p2 = f2;//C++ 11 utomatic type deduction17 //pre-C++11 can use the following code instead18 //const double *(*p2)(const double *,int) = f2;19 cout<<"Using pointers to functions:\n";20 cout<<"Address Value\n";21 cout<<(*p1)(av,3)<<":"<<*(*p1)(av,3)<<endl;22 cout<<p2(av,3)<<":"<<*p2(av,3)<<endl;23 24 //pa an array of pointers25 //auto doesn't work with list initialization26 const double *(*pa[3])(const double *,int) = {f1,f2,f3};27 //pb a pointer to first element of pa28 auto pb = pa;29 // pre-C++11 can use the following code instead30 // const double *(**pb)(const double *, int) = pa;31 cout<<"\nUsing an array of pointers to functions:\n";32 cout<<"Address Value\n";33 for(int i = 0;i < 3; i++)34 cout<<pa[i](av,3)<<":"<<*pa[i](av,3)<<endl;35 cout<<"\nUsing a pointer to a pointer to a function:\n";36 cout<<"Address Value\n";37 for(int i = 0;i < 3; i++)38 cout<<pb[i](av,3)<<":"<<*pb[i](av,3)<<endl;39 40 //what about a pointer to an array of function pointers41 cout<<"\nUsing pointers to an array of pointers:\n";42 cout<<"Address Value\n";43 //easy way to declare pc44 auto pc = &pa;45 // pre-C++11 can use the following code instead46 // const double *(*(*pc)[3])(const double *, int) = &pa;47 cout<<(*pc)[0](av,3)<<":"<<*(*pc)[0](av,3)<<endl;48 //hard way to declare pd49 const double *(*(*pd)[3])(const double *,int) = &pa;50 //store return value in pdb51 const double *pdb = (*pd)[1](av,3);52 cout<<pdb<<":"<<*pdb<<endl;53 //alternative notation54 cout<<(*(pd)[2])(av,3)<<":"<<*(*(*pd)[2])(av,3)<<endl;55 }56 57 const double * f1(const double * ar, int n)58 {59 return ar;60 }61 const double * f2(const double ar[], int n)62 {63 return ar+1;64 }65 const double * f3(const double ar[], int n)66 {67 return ar+2;68 }