函数指针和函数指针数组

定义

double * (*pa)(const *double, int); //返回值为double* 类型的函数指针pa
double * (*dp[3])(const *double, int); //返回值为double*类型的函数指针数组dp, dp[0], dp[1], dp[2]分别为三个函数指针

使用

直接上代码

#define debug(...) printf(__VA_ARGS__)

#include<iostream>
#include<array>

using namespace std;

const int Seasons = 4;
const array<string, Seasons> Snames = {"Spring", "Summer", "Fall", "Winter"};

double pam(int);
double pam1(int);
void estimate(int lines, double (*pf)(int));
const double * f1(const double ar[], int n);
const double * f2(const double [], int);
const double * f3(const double *, int);


int main(){
    const double * (*pa[3])(const double *, int) = {f1, f2, f3}; //包含三个函数指针的数组
    double av[3] = {1112.3, 1542.6, 2227.9};
    for(int i=0; i<3; ++i){
        cout << i << ": " << *pa[i](av, 0) << endl;
    }
    double (*pf)(int);
    pf = pam1;
    array<double, Seasons> expenses;
    estimate(50, pf);
    return 0;
}

double pam(int lns){
    return 0.03 * lns + 0.0004 * lns * lns;
}

double pam1(int lns){
    return 0.1 * lns;
}

void estimate(int lines, double (*pf)(int)){
    cout << lines << " lines will take ";
    cout << (*pf)(lines) << " hour(s)\n";
}

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值