您做错了一件事 - 您试图在调用“function”之前调用“ptr”函数。您应该做的就是仅传递一个指向“ptr”的指针,并使用从“function”传递的指针调用“ptr”,如下所示:
void ptr(int x)
{
printf("from ptr [%d]\n", x);
}
int function(int a, int b , void (*func)(int) )
{
printf( "from function a=[%d] b=[%d]\n", a, b );
func(a); // you must invoke function here
return 123;
}
void main()
{
function( 10, 2, &ptr );
// or
function( 20, 2, ptr );
}
这使:
from function a=[10] b=[2]
from ptr [10]
from function a=[20] b=[2]
from ptr [20]
这就是你想要的
for
function(20, 20, ptr(20));
工作 - 你必须有这样的东西:
// 'ptr' must return sth (int for example)
// if you want its ret val to be passed as arg to 'function'
// this way you do not have to invoke 'ptr' from within 'function'
int ptr(int);
int function(int, int , int);