Using void * in C in place of overloading?
c, function, void-pointers
Solution
yes, `void *` is great for the creation of generic functions. However, once you pass a pointer to a certain datatype to a function that takes a `void *` you lose all type information. One way to steer your program so it knows which type you passed in is to have a 2nd parameter of type `enum` which may have values such as INT, FLOAT, DOUBLE etc. etc.
#include <stdio.h>
typedef enum inputTypes
{
INT,
DOUBLE,
CHAR
} inType;
void myfunc(void*, inType);
int main(void)
{
int i = 42;
double d = 3.14;
char c = 'a';
myfunc(&i, INT);
myfunc(&d, DOUBLE);
myfunc(&c, CHAR);
return 0;
}
void myfunc(void* param, inType type)
{
switch(type) {
case INT:
printf("you passed in int %d\n", *((int *)param));
break;
case DOUBLE:
printf("you passed in double %lf\n", *((double *)param));
break;
case CHAR:
printf("you passed in char %c\n", *((char *)param));
break;
}
}
Output
you passed in int 42
you passed in double 3.140000
you passed in char a
Problem
My question here is I had seen code like this for a multithreading application: ``` void Thread( void* pParams ) { int *milliseconds = (int *)pParams; Sleep(milliseconds); printf("Finished after %d milliseconds", milliseconds); //or something like that } ``` This greatly took my interest, I knew `malloc` sends back a void pointer and you can cast it to what you want, does this mean that I can create a function that can accept any data type? For example a function I wrote without testing: ``` void myfunc( void* param ) { switch(sizeof(param)) { case 1: char *foo = (char *)param; break; case 2: short *foo = (short *)param; break; case 4: int *foo = (int *)param; break; } } myfunc(3.1415); myfunc(0); myfunc('a'); ``` I may be completely wrong, even if this does work is it horrible practise? Thanks.