As others mentioned already, at the end of the day you need to know the type of the variables in order to do something with them.
Besides void *
pointers, you may also treat everything as c-strings (nul
-terminated buffers of chars), then use any of the C99 strto*
family of functions (float*, signed*, unsigned*) to convert them to their intended type on-demand.
For example, the following declares a
as a c-string and outputs its float
, long int
and unsigned long
representations:
char *a = "-2.3";
printf( "float: %f\n", strtof(a, NULL) );
printf( "long: %ld\n", strtol(a, NULL, 10) );
printf( "unsigned long: %lu\n", strtoul(a, NULL, 10) );
Output:
float: -2.300000
long: -2
unsigned long: 4294967294
You can do more fancy stuff with those functions (and even more with void *
pointers) but neither of them answers exactly your question, because what you asked is not directly supported by C.
Both suggestions fight against the nature of the language. If you are to use C, just learn and use the supporting types (or define your own, which is also well supported).