You can do something like this :
void getData(void *Data , uint8_t outputType) {
if (outputType == 0) {
int16_t* data = Data;
/* fill int16_t data here - eg. : */
data[0] = 42;
}
else {
float* data = Data;
/* fill float data here - eg. : */
data[0] = 42.0f;
}
}
All assuming you call it either as :
int16_t output[3];
getData(output, 0);
or :
float output[3];
getData(output, 1);
Take special care not to exceed the bounds of the array.
Explanation
Any pointer to an object type can be converted to a void*
and back again to the original type, and the resulting pointer will compare equal to the original pointer.
This can be used (as in this case) to implement generic functions that can work on any pointer type (ref. eg. What does void* mean and how to use it?).
There is a caveat though : a void*
can't be dereferenced (nor can pointer arithmetic be performed on it), so it has to be converted back to the original pointer type before dereferencing. The outputType
parameter says exactly what that original pointer type was, so the function can perform the proper conversion.
After that, the function can work on the converted pointer as if it were the original pointer. If the original pointer points to an array, then the converted pointer can also be used to access all items of that array.