1

Let's say I have function accepting function pointer as argument.

void fun(void(*problematicFunPointer)(void*) problemFun){
...
}

Then, when I want to use this function like this:

void actualProblematicFun1(struct s_something * smt){
...
}

void actualProblematicFun2(struct s_something_else * smthels){
...
}

int main(){
    fun(&actualProblematicFun1);
    fun(&actualProblematicFun2);
}

When using -Werror, this throws compile error -Werror=incompatible-pointer-types. Is there a way to suppress warning for this particular case?

2
  • AFAIK, not without effectively casting the pointer to void(*)(void*). Calling the resultant pointer is then technically undefined behavior, although practically it works and is (AFAIK) the only way to get that better codegen. The C standard would want you to write a wrapper function and pass a pointer to that. Commented Jan 15, 2022 at 0:26
  • casting to void()(void) would help in this case. but in my project, I use more function types with more that 1 argument. for example void()(void, int , char), so I would have to cast all of them to their respective "voided" types. I took care to not mess up with possible undefined behavior when using function like this, so as you said, it should practicaly work. It is just problem that when I compile with -Werror, the output is flooded with this one warning and makes it hard to read. Commented Jan 15, 2022 at 0:38

1 Answer 1

1

Is there a way to suppress warning for this particular case?

Write the actual functions with proper argument type that matches the declaration.

void actualProblematicFun1(struct s_something * smt){
...
}

void actualProblematicFun2(struct s_something_else * smthels){
...
}

void actualProblematicFun1Trampoline(void *smt) {
   actualProblematicFun1(smt);
}

void actualProblematicFun2Trampoline(void *smthels) {
    actualProblematicFun2(smthels);
}


int main(){
    fun(&actualProblematicFun1Trampoline);
    fun(&actualProblematicFun2Trampoline);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.