c语言函数指针

  • 函数指针
    函数指针:函数名本身即函数指针,指向函数的入口地址。
    函数指针的定义:返回值类型 (*函数指针名)(形参列表类型) = 函数名;
    函数指针的调用:函数指针名(实参列表); 等价于函数名(实参列表);
#include <iostream>
int my_func(int a,int b)
{
    printf("ret: %d\n",a+b);
    return 0;
}

//1 先定义函数类型,通过类型定义指针
void test01()
{
    typedef  int(FUNC_TYPE)(int,int);
    FUNC_TYPE* f = my_func;
    // 调用的两种方式
    (*f)(10,20);
    f(10,30);
}
//2 定义函数指针类型
void test02()
{
    typedef  int(*FUNC_POINT)(int,int);
    FUNC_POINT f = my_func;
    // 调用的两种方式
    (*f)(10,20);
    f(10,30);
}
//3 直接定义函数指针变量
void test03()
{
    int(*f)(int,int)=my_func;
    // 调用的两种方式
    (*f)(10,20);
    f(10,30);
}
int main()
{
    test01();
    printf("===================\n");
    test02();                                                                                                     
    printf("===================\n");
    test03();
}

typedef 函数返回值类型 (* 指针变量名) (函数参数列表);

// 第一句就是定义了一种 ptrFunc 的类型,并定义这种类型为指向某种函数的指针后面就可以像使用int,char等变量类型名一样使用 ptrFunc 了
typedef int(*ptrFunc)(int,int);

[原文链接] https://blog.csdn.net/qq_36942720/article/details/120145016

posted on 2021-08-19 14:48  lodger47  阅读(79)  评论(0)    收藏  举报

导航