
casting void pointer to be a pointer to a function
Quote:
>A void pointer can contain a pointer to an object or a pointer to
>an incomplete type. That's all there is to it. Void pointers cannot
>contain a pointer to a function type, no matter whether or not they're
>small, near, far, big, huge, humonguous or bent.
The wonderful thing about C is with a pointer you can be
anywhere and just about anything, provided you have its
address. I would like to disagree with Jos because I
use void pointers to hide all sorts of evil things that
I am doing from the compiler. Here is a recent piece
of C code which shows how a function jump table was
implemented using an array of void pointers.
void handler1(void *data, int count)
{
Quote:
}
void handler2(void *data, int count)
{
Quote:
}
void handler3(void *data, int count)
{
Quote:
}
void move_data(int type, void *data, int count)
{
static void *jump_table[] =
{
(void *)handler1,
(void *)handler2,
(void *)handler3
};
(void (*)(void *, int))jump_table[type])(data, count);
Quote:
}