Hi I am initializing a function pointer to replace a switch statement in a boolean function. So i wanted to use a member of the structure and assign/copy address of a boolean function to this member. My later plan is to remove the switch cases and use function pointer to handle specific types (TYPE_A... etc)
//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;
}
I am stuck with the compilation error while assigning the address of a boolean function to a member of a struct. Any clue to solve this? Also node->tone is a wrong way to initialize. Tried memcpy or malloc. It didn't really work.
Thanks in Advance!
CodePudding user response:
The tone
function doesn't match the pointer type.
tone
is declared to return a bool_t
but the function pointer type tone_function_t
returns a bool
.
Change one of the two so the return types match.