今天看到了函数指针这一小结,发现自己对指针的认知还是不够深刻,特意贴下来,日后回顾。
《C++ Primer Plus》
第七章 7.19(arfupt.cpp)
#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 }; //p1 指向 f1 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; //注意()优先级大于*,*p2(av,3) == *(p2(av,3)) 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++) // (*(pa+i))(av,3) == pa[i](av,3) 似乎[]优先级高于() cout << (*(pa + i))(av,3) << ": " << *(*(pa+i))(av,3) << endl; cout << "\nUsing a pointer 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"; // 三级指针 const double *(*(*pd)[3])(const double *, int ) = & pa; auto pc = &pa; const double * pdb = (*pd)[1](av,3); cout << (*pc)[0](av,3) << ": " << *(*pc)[0](av,3) << endl; 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; }
第七章 复习题-13
#include <iostream> #include <cstring> //声明一个结构体 struct applicant { char name[30]; int credit_rating[3]; }; using namespace std; //声明一个show函数 void show(applicant *); //声明一个empty函数 void empty(const applicant *); //声明一个函数指针类型 typedef void (*tempty)(const applicant *); //声明一个指向empty函数的函数指针p1 tempty p1 = empty; //声明一个函数strscat const char * strscat(const applicant *, const applicant *); //声明一个函数指针p2,并指向strscat const char * (*p2)(const applicant *,const applicant *) = strscat; //声明一个包含5个函数指针的数组ap tempty ap [5]; //声明一个包含10个函数指针的数组pb const char * (*(pb[10]))(const applicant * ,const applicant *); //同上,声明一个包含10个函数指针的数组pc const char * (*pc[10])(const applicant * ,const applicant *); //声明一个指向包含10个函数指针的数组的指针pa char * (*(*pa[10]))(const applicant * ,const applicant *); int main() { struct applicant test = { "=-=.",{ 0,1,2 } }; cout << "Use show function." << endl; show(&test); cout << "Use strscat function." << endl; const char * pstr = strscat(&test,&test); cout << pstr << endl; //忘记添加的delete. =_=! delete [] pstr; return 0; } void show(struct applicant * app) { cout << app->name << endl; for (int i = 0; i < 3; i++) cout << app->credit_rating[i] << endl; return; } void empty(const applicant * a) { return; } const char * strscat(const applicant * chars1, const applicant * chars2) { char pstr [20]; strcpy(pstr,chars1->name); char * str = new char [40]; strcpy(str,strcat(pstr,chars2->name)); return str; }