[assuming all types of pointers including void *
have the same size]
Modify what you have like so
union img_rawdata {
void * untyped;
uint8_t * u8;
uint16_t * u16;
float * flt;
};
enum img_type {
IMG_UNDEF = -1
IMG_U8 = 0,
IMG_U16,
IMG_FLT,
IMG_MAX
};
and add
const size_t img_type_size[IMG_MAX] = {
sizeof(*((union img_rawdata *) NULL)->u8),
sizeof(*((union img_rawdata *) NULL)->u16),
sizeof(*((union img_rawdata *) NULL)->flt)
};
then replace the switch by:
assert(IMG_UNDEF < type && IMG_MAX > type);
img->rawdata.untyped = malloc(num_vals * img_type_size[type]);