c/c++ 函数指针的用法

refer: 原文链接 链接2


1.     定义
每一个函数都占用一段内存单元,它们有一个起始地址,指向函数入口地址的指针称为函数指针。
2.     语法
指向函数的指针变量的一般定义形式为:
数据类型 (*指针变量名)(参数表);
3.     说明
1) 函数指针的定义形式中的数据类型是指函数的返回值的类型。
2) 区分下面两个语句:
int (*p)(int a, int b); //p是一个指向函数的指针变量,所指函数的返回值类型为整型
int *p(int a, int b); //p是函数名,此函数的返回值类型为整型指针
3) 指向函数的指针变量不是固定指向哪一个函数的,而只是表示定义了一个这样类型的变量,它是专门用来存放函数的入口地址的;在程序中把哪一个函数的地址赋给它,它就指向哪一个函数。
4) 在给函数指针变量赋值时,只需给出函数名,而不必给出参数。
如函数max的原型为:int max(int x, int y); 指针p的定义为:int (*p)(int a, int b); 则p = max;的作用是将函数max的入口地址赋给指针变量p。这时,p就是指向函数max的指针变量,也就是p和max都指向函数的开头。
5) 在一个程序中,指针变量p可以先后指向不同的函数,但一个函数不能赋给一个不一致的函数指针(即不能让一个函数指针指向与其类型不一致的函数)。
如有如下的函数:int fn1(int x, int y); int fn2(int x);
定义如下的函数指针:int (*p1)(int a, int b); int (*p2)(int a);

p1 = fn1; //正确
p2 = fn2; //正确
p1 = fn2; //产生编译错误
6) 定义了一个函数指针并让它指向了一个函数后,对函数的调用可以通过函数名调用,也可以通过函数指针调用(即用指向函数的指针变量调用)。
如语句:c = (*p)(a, b); //表示调用由p指向的函数(max),实参为a,b,函数调用结束后得到的函数值赋给c。
7) 函数指针只能指向函数的入口处,而不可能指向函数中间的某一条指令。不能用*(p+1)来表示函数的下一条指令。
8) 函数指针变量常用的用途之一是把指针作为参数传递到其他函数。


c语言函数指针的定义形式:返回类型 (*函数指针名称)(参数类型,参数类型,参数类型,…);
c++函数指针的定义形式:返回类型 (类名称::*函数成员名称)(参数类型,参数类型,参数类型,….);    
以下代码编译环境:codeblocks with gcc in win 7


c语言函数指针使用举例:

#include <stdio.h>
#include <stdlib.h>

int fun1()
{
    printf("this is fun1 call\n");
    return 1;
}

void fun2(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

int main()
{
    int (*pfun1)() = NULL;
    void (*pfun2)(int, char) = NULL;
    int a,b;
    pfun1 = fun1; //第一种赋值方法
    a = pfun1();  //第一种调用方法(推荐)
    printf("%d\n",a);
    b = (*pfun1)();//第二种调用方法
    printf("%d\n",b);
    pfun2 = &fun2;//第二种赋值方法(推荐,因为和其他数据指针赋值方法一致)
    pfun2(1,'a');
    (*pfun2)(2,'b');
    return 0;
}


c++函数指针使用举例:
#include <iostream>
using namespace std;

class test
{
public:
    test()
    {
        cout<<"constructor"<<endl;
    }
    int fun1(int a, char c)
    {
        cout<<"this is fun1 call:"<<a<<" "<<c<<endl;
        return a;
    }
    void fun2(double d)const
    {
        cout<<"this is fun2 call:"<<d<<endl;
    }
    static double fun3(char buf[])
    {
        cout<<"this is fun3 call:"<<buf<<endl;
        return 3.14;
    }
};

int main()
{
    // 类的静态成员函数指针和c的指针的用法相同
    double (*pstatic)(char buf[]) = NULL;//不需要加类名
    pstatic = test::fun3; //可以不加取地址符号
    pstatic("myclaa");
    pstatic = &test::fun3;
    (*pstatic)("xyz");

    //普通成员函数
    int (test::*pfun)(int, char) = NULL; //一定要加类名
    pfun = &test::fun1; //一定要加取地址符号
    test mytest;
    (mytest.*pfun)(1, 'a'); //调用是一定要加类的对象名和*符号

    //const 函数(基本普通成员函数相同)
    void (test::*pconst)(double)const = NULL; //一定要加const
    pconst = &test::fun2;
    test mytest2;
    (mytest2.*pconst)(3.33);

//    //构造函数或者析构函数的指针,貌似不可以,不知道c++标准有没有规定不能有指向这两者的函数指针
//    (test::*pcon)() = NULL;
//    pcon = &test.test;
//    test mytest3;
//    (mytest3.*pcon)();

    return 0;
}


函数指针作为函数参数:
#include <stdio.h>
#include <stdlib.h>

void fun(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

void fun1(void (*pfun)(int, char), int a, char c)
{
    pfun(a, c);
}

int main()
{
    fun1(fun, 1, 'a');
    return 0;
}
// c++ 的形式差不多


函数指针作为函数返回值:
// c 形式
#include <stdio.h>
#include <stdlib.h>

void fun(int k, char c)
{
    printf("this is fun2 call:%d %c\n", k, c);
}

//fun1 函数的参数为double,返回值为函数指针void(*)(int, char)
void (*fun1(double d))(int, char)
{
    printf("%f\n",d);
    return fun;
}

int main()
{
    void (*p)(int, char) = fun1(3.33);
    p(1, 'a');
    return 0;
}

//c++ 形式
#include <iostream>
using namespace std;

class test
{
public:
    int fun(int a, char c)
    {
        cout<<"this is fun call:"<<a<<" "<<c<<endl;
        return a;
    }
};

class test2
{
    public:
    // test2 的成员函数fun1,参数是double,
    //返回值是test的成员函数指针int(test::*)(int, char)
    int (test::*fun1(double d))(int, char)
    {
        cout<<d<<endl;
        return &test::fun;
    }
};

int main()
{
    test mytest;
    test2 mytest2;
    int (test::*p)(int, char) = mytest2.fun1(3.33);
    (mytest.*p)(1, 'a');
    return 0;
}



函数指针数组:

#include <stdio.h>
#include <stdlib.h>

float add(float a,float b){return a+b;}
float minu(float a,float b){return a-b;}

int main()
{
    //定义一个函数指针数组,大小为2
    //里面存放float (*)(float, float)类型的指针
    float (*pfunArry[2])(float, float) = {&add, &minu};
    double k = pfunArry[0](3.33,2.22);// 调用
    printf("%f\n", k);
    k = pfunArry[1](3.33,2.22);
    printf("%f\n", k);
    return 0;
}
//c++ 可类比


typedef 简化函数指针类型:
#include <stdio.h>
#include <stdlib.h>

float add(float a,float b)
{
    printf("%f\n",a+b);
    return a+b;
}
float minu(float a,float b)
{
    printf("%f\n",a-b);
    return a-b;
}

//用pfunType 来表示float(*)(float, float)
typedef float(*pfunType)(float, float);

int main()
{
    pfunType p = &add;//定义函数指针变量
    p(3.33, 2.22);
    pfunType parry[2] = {&add, &minu};//定义函数指针数组
    parry[1](3.33, 2.22);
    //函数指针作为参数可以定义为:void fun(pfunType p)
    //函数指针作为返回值可以定义为:pfunType fun();

    return 0;
}
//c++ 可类比

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值