C 中的函数指针困难,int (*foo(enum int var))(int)
Function pointer difficulty in C, int (*foo(enum int var))(int)
查看此处的一些 OSCORE 代码:https://github.com/Fraunhofer-AISEC/uoscore-uedhoc/blob/main/modules/oscore/src/option.c 在第 31 行和第 63 行有一些不寻常的转换和指针函数工作,我可以使用帮助。
bool (*class_to_condition(enum option_class class))(uint16_t code)
{
switch (class) {
case CLASS_I:
return is_class_i; //is_class_x() returns bool
case CLASS_E:
return is_class_e;
default:
break;
}
return false;
}
以后
bool (*condition)(uint16_t) = class_to_condition(class);
然后用作
if (!condition(code)) {
我得到:
- class_to_condition(u16) 将 return 一个 bool
- “条件”变量是一个指向函数的指针class_to_condition,这个函数将接受一个参数
- uint16_t 是函数接受的参数类型
我不明白
- 枚举
- 为什么它似乎有两个参数(有点)
- 为什么函数定义本身就是一个指针
- uint16_t “代码”从未在 class_to_condition() 中使用过?
作为明显的非指针替代方案的指针是否更有效,值得增加复杂性(聪明 vs 可爱)?
函数class_to_condition
接受一个enum option_class
作为参数和returns一个函数指针。返回的函数指针指向一个函数,该函数具有 uint16_t
参数和 returns 一个 bool
.
如果我们添加以下 typedef:
typedef bool (*fptr)(uint16_t);
我们可以将函数签名重写为:
fptr class_to_condition(enum option_class class) {
调用如下:
fptr condition = class_to_condition(class);
class_to_condition
函数接受一个 enum option_class
类型的参数。它 returns 一个指向函数的指针,该函数接受一个 uint16_t
参数和 returns 一个 bool
.
使用类型别名会更清晰:
// Declare a -alias of a function pointer
typedef bool (*condition_function_type)(uint16_t);
condition_function_type class_to_condition(enum option_class class)
{
// ...
}
// ...
condition_function_type condition = class_to_condition(class);
The clockwise/spiral rule 可能有助于破译复杂的声明。
作为对@Someprogrammerdude 答案的补充,我不会亲自将指针隐藏在 typedef
后面
typedef bool condition_function_type(uint16_t);
condition_function_type *class_to_condition(enum option_class class)
{
// ...
}
// ...
condition_function_type *condition = class_to_condition(class);
IMO 更容易阅读和理解我们正在处理函数指针。
查看此处的一些 OSCORE 代码:https://github.com/Fraunhofer-AISEC/uoscore-uedhoc/blob/main/modules/oscore/src/option.c 在第 31 行和第 63 行有一些不寻常的转换和指针函数工作,我可以使用帮助。
bool (*class_to_condition(enum option_class class))(uint16_t code)
{
switch (class) {
case CLASS_I:
return is_class_i; //is_class_x() returns bool
case CLASS_E:
return is_class_e;
default:
break;
}
return false;
}
以后
bool (*condition)(uint16_t) = class_to_condition(class);
然后用作
if (!condition(code)) {
我得到:
- class_to_condition(u16) 将 return 一个 bool
- “条件”变量是一个指向函数的指针class_to_condition,这个函数将接受一个参数
- uint16_t 是函数接受的参数类型
我不明白
- 枚举
- 为什么它似乎有两个参数(有点)
- 为什么函数定义本身就是一个指针
- uint16_t “代码”从未在 class_to_condition() 中使用过?
作为明显的非指针替代方案的指针是否更有效,值得增加复杂性(聪明 vs 可爱)?
函数class_to_condition
接受一个enum option_class
作为参数和returns一个函数指针。返回的函数指针指向一个函数,该函数具有 uint16_t
参数和 returns 一个 bool
.
如果我们添加以下 typedef:
typedef bool (*fptr)(uint16_t);
我们可以将函数签名重写为:
fptr class_to_condition(enum option_class class) {
调用如下:
fptr condition = class_to_condition(class);
class_to_condition
函数接受一个 enum option_class
类型的参数。它 returns 一个指向函数的指针,该函数接受一个 uint16_t
参数和 returns 一个 bool
.
使用类型别名会更清晰:
// Declare a -alias of a function pointer
typedef bool (*condition_function_type)(uint16_t);
condition_function_type class_to_condition(enum option_class class)
{
// ...
}
// ...
condition_function_type condition = class_to_condition(class);
The clockwise/spiral rule 可能有助于破译复杂的声明。
作为对@Someprogrammerdude 答案的补充,我不会亲自将指针隐藏在 typedef
typedef bool condition_function_type(uint16_t);
condition_function_type *class_to_condition(enum option_class class)
{
// ...
}
// ...
condition_function_type *condition = class_to_condition(class);
IMO 更容易阅读和理解我们正在处理函数指针。