Passing user data to lv_obj_add_event_cb

Hi
I am to make a radio button from the check box. The matter I have faced is that I don’t know really how to pass user data arguments into the function.
The code I have used so far with loads of warnings that works is as follows.

lv_obj_t * ViewSelection[4];
static void VIEW_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    char userdata = lv_event_get_user_data(e);

    if(code == LV_EVENT_RELEASED)
    {
        switch(userdata)
        {
            case 1:
                lv_obj_clear_state(ViewSelection[1], LV_STATE_CHECKED);
                lv_obj_clear_state(ViewSelection[2], LV_STATE_CHECKED);
                lv_obj_clear_state(ViewSelection[3], LV_STATE_CHECKED);
                break;
            case 2:
                lv_obj_clear_state(ViewSelection[0], LV_STATE_CHECKED);
                lv_obj_clear_state(ViewSelection[2], LV_STATE_CHECKED);
                lv_obj_clear_state(ViewSelection[3], LV_STATE_CHECKED);
                break;
            case 3:
                lv_obj_clear_state(ViewSelection[0], LV_STATE_CHECKED);
                lv_obj_clear_state(ViewSelection[1], LV_STATE_CHECKED);
                lv_obj_clear_state(ViewSelection[3], LV_STATE_CHECKED);
                break;
            case 4:
                lv_obj_clear_state(ViewSelection[0], LV_STATE_CHECKED);
                lv_obj_clear_state(ViewSelection[1], LV_STATE_CHECKED);
                lv_obj_clear_state(ViewSelection[2], LV_STATE_CHECKED);
                break;
        }
    }
}

lv_obj_t* CreateScreenMenuView(void)
{
    ScreenMenu = lv_obj_create(NULL);
    lv_scr_load_anim(ScreenMenu,LV_SCR_LOAD_ANIM_FADE_ON, 200, 0, true);

    for (char i = 0;i<4;i++)
        {
            ViewSelection[i] = lv_checkbox_create(ScreenMenu);
            lv_obj_add_event_cb(ViewSelection[i],VIEW_event_handler,LV_EVENT_RELEASED,i+1);
        }
    lv_obj_set_pos(ViewSelection[0],100,100);
    lv_obj_align_to(ViewSelection[1],ViewSelection[0],LV_ALIGN_OUT_BOTTOM_LEFT,0,30);
    lv_obj_align_to(ViewSelection[2],ViewSelection[1],LV_ALIGN_OUT_BOTTOM_LEFT,0,30);
    lv_obj_align_to(ViewSelection[3],ViewSelection[2],LV_ALIGN_OUT_BOTTOM_LEFT,0,30);
    return ScreenMenu;
}

The warning says
" passing argument 4 of ‘lv_obj_add_event_cb’ makes pointer from integer without a cast [-Wint-conversion] "

The warning is caused by the fact that our APIs expect a pointer as user data, however, we do nothing with the pointer besides storing it and returning it to you, so you should be able to safely cast i to void * to get rid of the warning, and then cast the result of lv_event_get_user_data back to char.

Thank you.
But then it would through another warning which is
“warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]|”
“warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]|”
I am looking for a strong method. How should I do that?

To solve that warning, I think you can use uintptr_t or intptr_t instead of char. v8 doesn’t allow you to change the user data type directly.

The matter in general is casting, not what to cast.
I searched the forum and found some had used structs instead of pure integeres in this cse for user data.

The problem is solved by passing struct into the user data.

Any data type can be pass as ‘user_data’, the thing is that this data must be ‘static’ in the function where the call back function is registered … otherwise user data gets destroyed and the pointer ‘points’ to trash data… or make it global so it doesn’t get destroyed.
void myBtnEvent_cb(lv_event_t* e){

int * btn_no; // create a user data type pointer
btn_no = (int*)lv_event_get_user_data(e); // cast the return pointer to data type pointer
printf(“btn no. %d”, * btn_no); // use the pointer value…

}
void myButtons(){

// make variable static… ( or declare it outside the function as ‘global’)
static int btn2_no = 2;
lv_obj_t* btn2 = lv_btn_create(anyUserPlace);
// pass the pointer to call back function
lv_obj_add_event_cb(btn2, myBtnEvent_cb, LV_EVENT_PRESSED, &btn2_no);

}