【发布时间】:2021-11-16 06:28:47
【问题描述】:
您好,我正在初始化一个函数指针以替换布尔函数中的 switch 语句。所以我想使用结构的成员并将布尔函数的地址分配/复制给该成员。我后来的计划是移除开关盒并使用函数指针来处理特定类型(TYPE_A...等)
//Declaration of typedef as a boolean
typedef bool (*tone_function_t) (state_t *state, u8_t type);
//typedef structure
typedef struct node {
tone_function_t tone;
} node_t;
bool_t tone(state_t *state, u8_t type) {
switch (type) {
case TYPE_A :
case TYPE_B :
case TYPE_C :
case TYPE_D :
case TYPE_E :
return TRUE;
}
return FALSE;
}
int main(state_t *state) {
node_t node;
node.tone = &tone; //Compilation Error : assignment from incompatible pointer type. Am i doing any mistake here??
return 0;
}
在将布尔函数的地址分配给结构的成员时,我遇到了编译错误。有什么线索可以解决这个问题吗? node->tone 也是一种错误的初始化方式。 尝试过 memcpy 或 malloc。它并没有真正起作用。
提前致谢!
【问题讨论】: