If you want to pass an integer to the callback API that passes void *, it is assumed that you pass the address of the integer. Note that this may mean that you need to perform dynamic allocation:
int *foo = malloc(sizeof *foo);
*foo = 42;
register_callback(cbfunc, foo);
Then in the callback:
void cbfunc(void *arg)
{
int *n = arg;
switch (*n) {
case 42:
}
free(arg);
}
(You can force the integer to void *and back, but the conversions are defined according to the implementation. The void *to intptr_t/ uintptr_tto deviation void *is required to preserve the value, but the converse is not so. It's ugly, anyway.).
source
share