Can you mention some real-life example where void*
can’t work well?
I could imagine these 3 approaches to work with void*
- Use pointer to global/static data
static int id1 = 13;
lv_obj_add_event_cb(obj, func, &id1);
static int id2 = 45;
lv_obj_add_event_cb(obj, func, &id2);
- Use union for small types
typedef union {
uint8_t num;
void * ptr;
} my_type;
my_type t = {.num = 7};
lv_obj_add_event_cb(obj, func, (my_type) t);
- malloc data with any type
my_type * t = malloc(sizeof(my_type));
t->x = ...;
lv_obj_add_event_cb(obj, func, t); //Free `t` in `LV_EVENT_DELETED`
What you can’t do is storing a larger struct as user data (not a pointer). But it’s not recommended anyway because it will consume memory for every callback.