In our code there is a function defined as:
a.h
extern int (*get_prof_action(void))(void);
a.c
static int (*ProfAction)(void) = NULL;
int (*get_prof_action(void))(void)
{
return ProfAction;
}
Then later in main.c, I want to gain this function through dlsym(I have to), so I used like this.
...
int (*p_get_prof_action(void))(void);
...
void my_function(){
...
void *handle=dlopen(mylibrary,RTLD_LAZY);
p_get_prof_action = (int (*(void))(void)) dlsym(handle,"get_prof_action");
...
}
And I got compile error:
error: cast specifies function type at line
(int (*(void))(void)) dlsym(handle,"get_prof_action");
=======================================================
Here are my questions:
This is not my code and I never saw such usage to define a function. Could you let me know what is this?
How do I correctly get this p_get_prof_action?
Thanks very much. This really stucks me a lot!!
You've declared
p_get_prof_actionas a function, not a function pointer. The correct declaration would be:And the correct cast would be:
When dealing with function pointers, a
typedefcan be very helpful. The function in question is returning a function pointer, so let's make one for the return type:Then declare your function pointer to return that type:
And assign to it like this: