函数指针和函数指针数组用法及其类型打印

写在前面

本篇博客主要介绍函数指针的用法,并设计程序验证函数名取地址,解引用等的异同。

函数指针

先看一段验证程序:

#include <iostream>
#include <typeinfo>

void print(void) 
{
    std::cout << "Hello, world\n";
}
void func1(void) 
{
    void (*p1)(void) = print;
    void (*p2)(void) = &print;
    void (*p3)(void) = *print;
    p1();
    p2();
    p3();
    auto q1 = print;
    auto q2 = &print;
    auto q3 = *print;
    std::cout << "q1:" << q1 << ", type: " << typeid(q1).name() << std::endl;
    std::cout << "q2:" << q2 << ", type: " << typeid(q2).name() << std::endl;
    std::cout << "q3:" << q3 << ", type: " << typeid(q3).name() << std::endl;
}

int main()
{
    func1();
    return 0;
}

程序运行的结果是:

Hello, world
Hello, world
Hello, world
q1:1, type: PFvvE
q2:1, type: PFvvE
q3:1, type: PFvvE

观察结果,我们不难得出,通过取地址、解引用获取的指针是一样的,并且指针的类型也是一样的,这篇博客中的论点就有些偏差了,评论中同学给出的观点可以验证,那么总结之,函数我们把它看成类似数组名一样的变量,对数组名取地址和数组名是一样的。

OK,至此我们可能想看一下类型"PFvvE"具体表示的是什么名字,我们可以参考reference typeidstackoverflow,当然也可以在visual studio IDE环境中查看变量类型。

$ echo "PFvvE" | c++filt --types
void (*)()

函数指针数组

函数指针数组可参见函数指针和函数指针数组及其应用这篇博客,总结之,假设函数指针的类型是void (*func)(void),那么对于的函数指针数组形式即为:void (*funcArr[3])(void),见下面代码:

void func1(void) {}
void func2(void) {}
void func3(void) {}

void (*p[3])(void) = {func1, func2, func3};

for (size_t i = 0; i < sizeof(p) / sizeof(p[0]); ++i) 
{
    p[i]();
}

————————

总结

  1. 函数指针的形式 typedef int (*fun_ptr)(int,int);;
  2. 对应的函数指针形式,int (*fun_ptr_arr[size])(int,int);
  3. 函数指针赋值时,推荐直接用函数名,因为C/C++标准不推荐对函数名,数组名取地址,容易引起语义模糊。

6.5.3.2 Address and indirection operators
Some implementations have not allowed the & operator to be applied to an array or a function. (The construct was permitted in early versions of C, then later made optional.) The C89 Language Committee endorsed the construct since it is unambiguous, and since data abstraction is enhanced by allowing the important & operator to apply uniformly to any addressable entity.

————————

其他参考资料

  1. 函数指针
  2. C/C++获取变量类型并输出
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章