相关疑难解决方法(0)

指向常量函数的指针的含义是什么?

指针可以声明为指向可变(非常量)数据或指向常量数据的指针.
可以将指针定义为指向函数.

我的同事和我正在讨论使用带有指针的"const",并且出现了关于使用const函数指针的问题.

以下是一些问题:

  1. 指向常量函数的指针与指向非常量函数的指针的含义是什么?
  2. 函数可以是常量吗?
  3. 函数可以是非const(可变)吗?
  4. 传递函数指针的正确(安全)语法是什么?

编辑1:函数指针语法

typedef void (*Function_Pointer)(void); // Pointer to void function returning void.

void function_a(Function_Pointer p_func); // Example 1.
void function_b(const Function_Pointer p_func); // Example 2.
void function_c(Function_Pointer const p_func); // Example 3.
void function_d(const Function_Pointer const p_func); // Example 4.
Run Code Online (Sandbox Code Playgroud)

上述声明是将函数指针视为指向内部类型的指针的示例.

数据,变量或存储器指针允许上述组合.
所以问题是:函数指针是否具有相同的组合以及指向const函数的指针(例如示例2)是什么意思?

c function-pointers const-correctness

26
推荐指数
2
解决办法
2万
查看次数

标签 统计

c ×1

const-correctness ×1

function-pointers ×1