#include<iostream>
const double* f1(const double ar[], int n);
const double* f2(const double[], int);
const double* f3(const double*, int);
int main()
{
using namespace std;
double av[3] = { 1112.3,1542.6,2227.9 };
const double* (*p1)(const double*, int) = f1;
auto p2 = f2;
cout << "using pointers to functions:\n";
cout << "address value\n";
cout << (*p1)(av, 3) << ":" << *(*p1)(av, 3) << endl;
cout << p2(av, 3) << ":" << *p2(av, 3) << endl;
const double* (*pa[3])(const double*, int) = { f1,f2,f3 };
auto pb = pa;
cout << "\nusing an array of pointers to functions:\n";
cout << "address value\n";
for (int i = 0; i<3; i++)
cout << pb[i](av, 3) << ":" << *pb[i](av, 3) << endl;
cout << "\nusing apointer to a pointer to a function:\n";
cout << "address value\n";
for (int i = 0; i < 3; i++)
cout << pb[i](av, 3) << ":" << *pb[i](av, 3) << endl;
cout << "\nusing pointers to an array of pointers:\n";
cout << "address value\n";
auto pc = &pa;
cout << (*pc)[0](av, 3) << ":" << *(*pc)[0](av,3) << endl;
const double*(* (*pd)[3])(const double*, int) = &pa;
const double* pdb = (*pd)[1](av, 3);
cout << pdb << ":" << *pdb << endl;
cout << (*(*pd)[2])(av, 3) << ":" << *(*(*pd)[2])(av, 3) << endl;
return 0;
}
const double* f1(const double* ar, int n)
{
return ar;
}
const double* f2(const double ar[], int n)
{
return ar + 1;
}
const double* f3(const double ar[], int n)
{
return ar + 2;
}
不要想复杂了,就是一个指针而已。应该说是函数指针,而不是指针函数。
本质上都是指针,只是指针的类型不一样,比如有int型 ,char型等指针,指向一个函数的指针就是函数指针。
int *p 整型指针,可以指向一个整型变量的地址。int a; p = &a
int (*p) (char * str)函数指针,可以指向一个返回整型,且有一个char *参数的函数。 int func(char * str); p = func 。要注意的就是函数指针的返回类型和参数要和指向的函数相同。