I know that a variable (void *) is guaranteed to be able to store any
pointer and to allow conversion back to the original pointer.
I think, then, that I can store a pointer to a function in a (void *)
variable and then convert it back to the original type and use it as a
funtion.
The question is: am I correct or there might be situations where this
could fail?
To clarify, here is an example of what I'm trying to do:
/* Prototypes of functions that are defined somewhere */
int f1(int x);
void f2(int a, int b);
/* Variables of type "pointers to functions" */
int (*fa)(int);
void (*fb)(int, int);
/* An array of generic pointers */
void *fun_vec[2];
..... /* later in the code ... */
fun_vec[0] = f1; /* store pointers to f1 and f1 */
fun_vec[1] = f2; /* into void * */
..... /* even later ... */
fa = fun_vec[0]; /* get them back as pointers */
fb = fun_vec[1]; /* to functions */
fb(2,fa(3)); /* use the functions */
....
pointer and to allow conversion back to the original pointer.
I think, then, that I can store a pointer to a function in a (void *)
variable and then convert it back to the original type and use it as a
funtion.
The question is: am I correct or there might be situations where this
could fail?
To clarify, here is an example of what I'm trying to do:
/* Prototypes of functions that are defined somewhere */
int f1(int x);
void f2(int a, int b);
/* Variables of type "pointers to functions" */
int (*fa)(int);
void (*fb)(int, int);
/* An array of generic pointers */
void *fun_vec[2];
..... /* later in the code ... */
fun_vec[0] = f1; /* store pointers to f1 and f1 */
fun_vec[1] = f2; /* into void * */
..... /* even later ... */
fa = fun_vec[0]; /* get them back as pointers */
fb = fun_vec[1]; /* to functions */
fb(2,fa(3)); /* use the functions */
....
Comment