【C++】函数和指针

时间:2023-03-09 06:00:31
【C++】函数和指针

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

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

1. 如何声明函数指针?

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

double pam(int); //参数为int 类型,返回值为double 类型的函数
double (*pf);(int) //指向参数为int类型,返回值为double 类型的指针
pf = pam; //函数名代表了函数的地址 double x = pam(); //函数名调用
double x = (*pf)(); //指针调用
double x = pf(); //C++也允许将指针名当作函数名使用

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

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. 将指针名当作函数名使用

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

4.  函数指针数组

const double *(*pa[]) (const double *,int) = {f1,f2,f3}; //创建函数指针数组
//通过指针调用函数,得到返回的指针
const double *px = pa[](av,); //call by pointer as if it were a function name
const double *py = (*pa[])(av,); //正常调用 //得到函数返回指针指向的值
double x = *pa[](av,);
double x = *(*pa[])(av,);

5. 指向指针数组的指针

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

*pd[] //an array of 3 pointers
(*pd)[] //a pointer to an array of three elements

指向数组的指针

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

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

 **&pa = *pa = pa[]

代码:

 //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[] = {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 Value\n";
cout<<(*p1)(av,)<<":"<<*(*p1)(av,)<<endl;
cout<<p2(av,)<<":"<<*p2(av,)<<endl; //pa an array of pointers
//auto doesn't work with list initialization
const double *(*pa[])(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 Value\n";
for(int i = ;i < ; i++)
cout<<pa[i](av,)<<":"<<*pa[i](av,)<<endl;
cout<<"\nUsing a pointer to a pointer to a function:\n";
cout<<"Address Value\n";
for(int i = ;i < ; i++)
cout<<pb[i](av,)<<":"<<*pb[i](av,)<<endl; //what about a pointer to an array of function pointers
cout<<"\nUsing pointers to an array of pointers:\n";
cout<<"Address Value\n";
//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)[](av,)<<":"<<*(*pc)[](av,)<<endl;
//hard way to declare pd
const double *(*(*pd)[])(const double *,int) = &pa;
//store return value in pdb
const double *pdb = (*pd)[](av,);
cout<<pdb<<":"<<*pdb<<endl;
//alternative notation
cout<<(*(pd)[])(av,)<<":"<<*(*(*pd)[])(av,)<<endl;
} const double * f1(const double * ar, int n)
{
return ar;
}
const double * f2(const double ar[], int n)
{
return ar+;
}
const double * f3(const double ar[], int n)
{
return ar+;
}