将返回值存储在函数指针数组的指针中是如何工作的?
How does Storing a return value in a pointer from a pointer -to- array of function pointers work?
这个例子(7.19(取自Stephen Prata C++ Primer 6th ed。
#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 function
const double *(*p1)(const double *, int) = f1;
auto p2 = f2; // C++11 automatic 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 };
// but it does work for initializing to a single value
// 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 pb
const double * pdb = (*pd[1]) (av, 3);
cout << pdb << ": " << *pdb << endl;
// alternative notation
cout << (*(*pd)[2])(av, 3) << ": " << *(*(*pd[2]))(av, 3) << endl;
// cin.get();
return 0;
}
// some rather dull functions
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;
}
指针*pdb是怎么回事?指针是获取整个数组还是仅获取一个元素?当程序运行时,在声明*pdb的位置会引发异常。谁能告诉我里面发生了什么?
pdb
只是一个指针,就像其他任何指针一样。它通过函数调用返回,以f2
.
由于它只是一个指针,因此它指向一个元素。这取决于你,取决于你调用的函数及其协定,知道之后是否还有元素。
相关文章:
- 对象指针在c++中是如何工作的
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 在C++中释放内存期间,迭代器与指针有何不同
- 为什么指针在对二维数组进行排序时无法正常工作?
- 为什么 C 样式字符串的工作空指针检查不?
- 像自定义类一样构造的指针(内置类型)如何工作?
- 为什么通过定义另一个指针单元格,整个代码停止工作?
- 在不工作的情况下为数组分配指针,但反过来也可以
- std::function是如何使指向成员函数的指针工作的
- 为什么运算符"new"需要指针才能工作?
- 当删除 [] 指针工作时,为什么无法获得指向的数组大小?
- For 循环使用指针遍历数组无法正常工作
- 指向成员函数的指针如何工作
- 指向成员结构的指针如何工作
- c++在函数参数中使用**指针不工作
- 指向其他类(boost::variate_generator)的指针不工作
- 多线程 - 为什么在引用上工作不会改变它,而在指针上工作正常
- 如何使重载操作符与指针一起工作
- c++指针不工作
- 这个StackOverFlowException要求指针正确工作吗?