I have been given a code. I need to use the following function but I am having problems calling it.
int myFunction(const struct LQuery *pQuery,
f_Callback Callback,
const void *pPriv);
And I have the following definition
typedef void (f_Callback) (int NumOfRecordsFound,
void *pPriv,
const tL_QueryResult *pData);
I understand that Callback
should be the the pointer to my_callbackFunction
but I can't seem to understand how to set the parameters to it. As far as I understand pQuery
is passed to the callback function but f_Callback
requires 3 parameters.
ie
int main (int Argc, char *pArgv[])
{
myFunction(what should go here and WHY)
}
The callback function will be called from the myFunction
. You have to pass just your callback function as a parameter. Here's an example:
#include <stdio.h>
typedef void (f_Callback)(int a,
int b,
int c);
int myFunction(int a, f_Callback* callback)
{
callback(a, a+1, a+2);
}
void myCallback(int a, int b, int c)
{
printf("a: %d, b: %d, c: %d\n", a, b, c);
}
int main()
{
myFunction(5, &myCallback);
return 0;
}
Expected output:
$./a.out
a: 5, b: 6, c: 7
The typedef is incorrect. It should be
typedef void (*f_Callback) (
int NumOfRecordsFound,
f_Callback Callback,
const void *pPriv);
which is a proper pointer-to-function - note the star; otherwise it will declare a typedef for a function prototype, which you cannot use as a variable as such, though it will work in a function declaration.
If you have a function like
int myFunction(const struct LQuery *pQuery,
f_Callback Callback,
const void *pPriv);
And a callback
void my_callback_function(int records_found,
f_Callback callback,
const void *priv)
{
...
}
You can call myfunction
simply as
myFunction(the_query, my_callback_function, the_private_pointer);
The lvalue my_callback_function
will decay into a pointer-to-function, so no &
character is needed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.