我是靠谱客的博主 朴实御姐,这篇文章主要介绍C++函数与指针,现在分享给大家,希望可以做个参考。

最近在看C++ primer plus,感觉函数与指针这一章难点比较多,记写笔记,加强理解.

From C++ Primer Plus: Chapter 7 Function:C++ Programming Modules

1. 如何声明函数指针?

和函数原型类似: 需要声明指针指向函数的返回值和参数列表


复制代码
1
2
3
4
5
6
7
double pam(int); //参数为int 类型,返回值为double 类型的函数 double (*pf);(int) //指向参数为int类型,返回值为double 类型的指针 pf = pam; //函数名代表了函数的地址 double x = pam(4); //函数名调用 double x = (*pf)(4); //指针调用 double x = pf(4); //C++也允许将指针名当作函数名使用
登录后复制

2. C++ 11 自动类型推断


复制代码
1
2
3
const double * f1(const double *, int); const double * (*p1)(const double *, int); //p1 poitns to f1 auto p2 = f1; //C++11 automatic type deduction,p2 points to f1 as well
登录后复制

3. 将指针名当作函数名使用


复制代码
1
2
3
4
//前面函数为double *类型,cout第一部分返回double指针,第二部分返回double指针指向的值 cout<<(*p1)(av,3)<<":"<<*(*p1)(av,3)<<endl; //和上面的cout一样只不过是使用函数指针名来调用函数 cout<<p2(av,3)<<":"<<*p2(av,3)<<endl;
登录后复制

4. 函数指针数组


复制代码
1
2
3
4
5
6
7
8
const double *(*pa[3]) (const double *,int) = {f1,f2,f3}; //创建函数指针数组 //通过指针调用函数,得到返回的指针 const double *px = pa[0](av,3); //call by pointer as if it were a function name const double *py = (*pa[0])(av,3); //正常调用 //得到函数返回指针指向的值 double x = *pa[0](av,3); double x = *(*pa[0])(av,3);
登录后复制

5. 指向指针数组的指针

指针数组和数组指针的区别


复制代码
1
2
*pd[3] //an array of 3 pointers (*pd)[3] //a pointer to an array of three elements
登录后复制

指向数组的指针


复制代码
1

登录后复制

1 auto pc = &pa; //&pa是整个数组的地址, pa是数组第一个元素首地址

2

3 const double * (*(*pd)[3])(const double *, int ) = &pa; //和第一个等价

4

5 **&pa = *pa = pa[0]

代码:


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//arfupt.cpp -- an array of function pointers #include<iostream> //various notations,same signatures 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}; //pointer to a function const double *(*p1)(const double *,int) = f1; auto p2 = f2;//C++ 11 utomatic type deduction //pre-C++11 can use the following code instead //const double *(*p2)(const double *,int) = f2; cout<<"Using pointers to functions:n"; cout<<"Address Valuen"; cout<<(*p1)(av,3)<<":"<<*(*p1)(av,3)<<endl; cout<<p2(av,3)<<":"<<*p2(av,3)<<endl; //pa an array of pointers //auto doesn't work with list initialization const double *(*pa[3])(const double *,int) = {f1,f2,f3}; //pb a pointer to first element of pa auto pb = pa; // pre-C++11 can use the following code instead // const double *(**pb)(const double *, int) = pa; cout<<"nUsing an array of pointers to functions:n"; cout<<"Address Valuen"; for(int i = 0;i < 3; i++) cout<<pa[i](av,3)<<":"<<*pa[i](av,3)<<endl; cout<<"nUsing a pointer to a pointer to a function:n"; cout<<"Address Valuen"; for(int i = 0;i < 3; i++) cout<<pb[i](av,3)<<":"<<*pb[i](av,3)<<endl; //what about a pointer to an array of function pointers cout<<"nUsing pointers to an array of pointers:n"; cout<<"Address Valuen"; //easy way to declare pc auto pc = &pa; // pre-C++11 can use the following code instead // const double *(*(*pc)[3])(const double *, int) = &pa; cout<<(*pc)[0](av,3)<<":"<<*(*pc)[0](av,3)<<endl; //hard way to declare pd const double *(*(*pd)[3])(const double *,int) = &pa; //store return value in pdb const double *pdb = (*pd)[1](av,3); cout<<pdb<<":"<<*pdb<<endl; //alternative notation cout<<(*(pd)[2])(av,3)<<":"<<*(*(*pd)[2])(av,3)<<endl; } 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; }
登录后复制

以上就是C++函数与指针的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是朴实御姐最近收集整理的关于C++函数与指针的全部内容,更多相关C++函数与指针内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(107)

评论列表共有 0 条评论

立即
投稿
返回
顶部