I've written a function that interprets serial data (CAN) and currently returns a float. I'd like the function to include an argument wherein the user specifies a return type in a string, and the function returns a value of that type. It's just a convenience thing, to avoid having to write multiple functions that share almost all of the same code.
Pass a void pointer to the type of data you want returned.
void foo(char* szType, void *pOut) {
switch (szType[0]) {
case 'I': *(int*)pOut = 1; break;
case 'F': *(float*)pOut = 1; break;
}
}
use like this:
int a;
float b;
foo("I", &a);
foo("F", &b);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With