nic*_*ame 0 c function-pointers ansi
我有这些原型:
int *my_func(int x, void (*other_func)(int a, int b));
int func2(int val1, int val2);
Run Code Online (Sandbox Code Playgroud)
假设有一个匹配它的函数.
如果我想实际调用my_func,我该怎么做?我试过以下没有运气:
my_func(1,func2);
Run Code Online (Sandbox Code Playgroud)
这是错误:
warning: passing argument 2 of ‘my_func’ from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)
那是因为函数原型不匹配:
void (*other_func)(void *a, int *b)
Run Code Online (Sandbox Code Playgroud)
是不一样的:
int func2(int val1, int val2);
Run Code Online (Sandbox Code Playgroud)
一个需要void*和int*,而其他需要两int秒.
编辑:
此外,返回类型不匹配.
编辑2:
既然你已经修复了这两个错误,那么这个答案是脱离背景的.我刚用这两个修复程序对它进行了测试,它编译:
int *my_func(int x, void (*other_func)(int a, int b)){
return 0;
}
void func2(int val1, int val2){
printf("blah");
}
int main(){
my_func(1,func2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)