美文网首页
函数指针

函数指针

作者: Jesson3264 | 来源:发表于2024-09-17 13:59 被阅读0次

函数指针基础:

  1. 获取函数的地址
  2. 声明一个函数指针
    3.使用函数指针来调用函数

获取函数指针:

函数的地址就是函数名,要将函数作为参数进行传递,必须传递函数名。

声明函数指针

声明指针时,必须指定指针指向的数据类型,同样,声明指向函数的指针时,必须指定指针指向的函数类型,这意味着声明应当指定函数的返回类型以及函数的参数列表。

例如:

double cal(int); // prototype
double (*pf)(int);  // 指针pf指向的函数, 输入参数为int,返回值为double
pf = cal;    // 指针赋值

如果将指针作为函数的参数传递:

void estimate(int lines, double (*pf)(int));  // 函数指针作为参数传递 

使用指针调用函数

double y = cal(5);   // 通过函数调用
double y = (*pf)(5);   // 通过指针调用 推荐的写法 
double y = pf(5);     // 这样也对, 但是不推荐这样写 

函数指针的使用:

#include <iostream>
#include <algorithm>
#include <cmath>
 
using namespace std;
 
double cal_m1(int lines)
{
    return 0.05 * lines;
} 
 
double cal_m2(int lines)
{
    return 0.5 * lines;
}
 
void estimate(int line_num, double (*pf)(int lines))
{
    cout << "The " << line_num << " need time is: " << (*pf)(line_num) << endl; 
}
 
 
 
int main(int argc, char *argv[])
{
    int line_num = 10;
    // 函数名就是指针,直接传入函数名
    estimate(line_num, cal_m1);
    estimate(line_num, cal_m2); 
    return 0;
}

函数指针数组:
这部分非常有意思:

#include <iostream>
#include <algorithm>
#include <cmath>
 
using namespace std;
 
// prototype   实质上三个函数的参数列表是等价的 
const double* f1(const double arr[], int n);
const double* f2(const double [], int);
const double* f3(const double* , int);
 
 
 
int main(int argc, char *argv[])
{
    double a[3] = {12.1, 3.4, 4.5};
    
    // 声明指针
    const double* (*p1)(const double*, int) = f1;
    cout << "Pointer 1 : " << p1(a, 3) << " : " << *(p1(a, 3)) << endl;
    cout << "Pointer 1 : " << (*p1)(a, 3) << " : " << *((*p1)(a, 3)) << endl;
    
    const double* (*parray[3])(const double *, int) = {f1, f2, f3};   // 声明一个指针数组,存储三个函数的地址 
    cout << "Pointer array : " << parray[2](a, 3) << " : " << *(parray[2](a, 3)) << endl;
    cout << "Pointer array : " << parray[2](a, 3) << " : " << *(parray[2](a, 3)) << endl;
    cout << "Pointer array : " << (*parray[2])(a, 3) << " : " << *((*parray[2])(a, 3)) << endl;
    
    return 0;
}
 
 
const double* f1(const double arr[], int n)
{
    return arr;     // 首地址 
} 
 
const double* f2(const double arr[], int n)
{
    return arr+1;
}
 
const double* f3(const double* arr, int n)
{
    return arr+2;
}
 

这里可以只用typedef来减少输入量:

typedef const double* (*pf)(const double [], int);  // 将pf定义为一个类型名称;
pf p1 = f1;
pf p2 = f2;
pf p3 = f3;

相关文章

  • 函数和指针

    函数指针: 指向函数的指针(是指针)指针函数:返回值是指针的函数(是函数)

  • 函数指针

    概念: 指针函数, 函数指针, 指针数组, 数组指针, 指向数组的指针, 指向函数指针数组的指针。

  • 指针

    一. 指针指向的是对象的地址//函数指针:指针指向函数//指针函数:函数返回指针 二.

  • C:函数指针的坑

    关于该死的函数指针和指针函数 先来个目录 常量指针、指针常量 数组指针、指针数组 函数指针、指针函数 1、先看第一...

  • C语言基础知识点

    函数指针与回调函数 1、函数指针:函数指针是指向函数的指针变量,以下实例声明了函数指针变量 p,指向函数 max:...

  • NDK启航篇——C语言基础(函数指针)

    昨天介绍了指针类型、空指针、指针运算,今天来写一下函数指针 函数指针 函数指针的定义:函数的返回值类型(函数指针的...

  • C语言基础---函数指针和回调函数

    版权声明:本文为小斑马伟原创文章,转载请注明出处!函数指针:函数指针 是指向函数的指针。指针函数:指针函数 函数...

  • Redis

    1.指针函数与函数指针 指针函数本质是指针,其返回值是指针。如 float *fun(); 函数指针,本质是指针。...

  • C++:函数指针 & 返回函数指针的函数

    函数指针 & 返回函数指针的函数 一、函数指针的声明和使用 声明一个函数指针,给它赋值并调用它指向的函数 函数指针...

  • 函数指针

    一.函数指针,指针指向函数 二.函数指针做参数

网友评论

      本文标题:函数指针

      本文链接:https://www.haomeiwen.com/subject/lfygljtx.html