Create object in method and get its pointers

hello everyone ive faced problem with this task

i have method that creates buttons with labels and returns label pointer as return value. so i can change its text and implement pushbutton callback.

also i am trying to work with calendar object, but when i am trying to (for example) lv_set_pos. ive got segmentation fault.

I cant find how to work with objects in lvgl properly, As example here`s code

(return btn) btn pointer is a global var so i can work with it later.

lv_obj_t * createstyledbutton_ret_btn(lv_obj_t * btn,lv_obj_t * par,lv_style_t* style, lv_coord_t pos_x,lv_coord_t pos_y,lv_coord_t size_wide,lv_coord_t size_height,char btn_text[],lv_event_cb_t event_call_back_function)
{
  btn = lv_btn_create(par, NULL); 
  lv_obj_add_style( btn, LV_OBJ_PART_MAIN,style);
  lv_obj_add_protect(btn, LV_PROTECT_CLICK_FOCUS);
  lv_obj_align(btn, NULL, LV_ALIGN_CENTER, 0, 0);
  lv_obj_set_pos(btn,  pos_x,  pos_y);                           
  lv_obj_set_size(btn,size_wide, size_height);                     
  lv_obj_set_event_cb(btn, event_call_back_function);                 /*Assign a callback to the button*/
  lv_obj_t * label = lv_label_create(btn, NULL);          /*Add a label to the button*/
  // lv_obj_align( label, NULL, allign, 0, 0 );
  lv_label_set_text(label, btn_text);                     /*Set the labels text*/
  return btn;
  `}

also i have calendar init method (where ive added return and input pointer but if i try to change calendar`s settings, ive got segmentation fault)

lv_obj_t* lv_ex_calendar_2(lv_obj_t* calendar)
{
    calendar = lv_calendar_create(tab0, NULL);
    lv_obj_set_size(calendar, 600, 600);
    //lv_obj_align(calendar, NULL, LV_ALIGN_CENTER, 0, 0);
   lv_obj_set_pos(calendar,-600,10);
    lv_obj_set_event_cb(calendar, calendar_event_handler);

    /*Make the date number smaller to be sure they fit into their area*/
 lv_obj_set_style_local_text_font(calendar, LV_CALENDAR_PART_DATE, LV_STATE_DEFAULT, lv_theme_get_font_small());

    /*Set today's date*/
    lv_calendar_date_t today;
    today.year = 2021;
    today.month = 10;
    today.day = 23;

    lv_calendar_set_today_date(calendar, &today);
    lv_calendar_set_showed_date(calendar, &today);
   return calendar;
}

but i am using pointers as input. so should i use pointers to pointers? seems method creates own pointer(different from global)

seems i need another implementation, where assignment happends after lvgl creates pointer to object.
so
1st is create object by lvgl
2nd assign address that stored in lvgl created pointer to global pointer of targeted object.

lv_obj_t * createstyledbutton_ret_btn(lv_obj_t * global_btn_pointer,lv_obj_t * par,lv_style_t* style, lv_coord_t pos_x,lv_coord_t pos_y,lv_coord_t size_wide,lv_coord_t size_height,char btn_text[],lv_event_cb_t event_call_back_function)
{
  btn = lv_btn_create(par, NULL); 
global_button_pointer=btn;
  lv_obj_add_style( btn, LV_OBJ_PART_MAIN,style);
  lv_obj_add_protect(btn, LV_PROTECT_CLICK_FOCUS);
  lv_obj_align(btn, NULL, LV_ALIGN_CENTER, 0, 0);
  lv_obj_set_pos(btn,  pos_x,  pos_y);                           
  lv_obj_set_size(btn,size_wide, size_height);                     
  lv_obj_set_event_cb(btn, event_call_back_function);                 /*Assign a callback to the button*/
  lv_obj_t * label = lv_label_create(btn, NULL);          /*Add a label to the button*/
  // lv_obj_align( label, NULL, allign, 0, 0 );
  lv_label_set_text(label, btn_text);                     /*Set the labels text*/
  return btn;
  `}

not working(
code of button that hides calendar

static void set_date_btn_cb(lv_obj_t * btn, lv_event_t event)
{ 
lv_btn_state_t btnstate=lv_btn_get_state(btn);
if (btnstate==LV_BTN_STATE_CHECKED_RELEASED&&event == LV_EVENT_RELEASED){
lv_obj_set_pos(calendar_obj,10,5);
lv_obj_move_foreground(calendar_obj);
}
else if (btnstate==LV_BTN_STATE_RELEASED&&event == LV_EVENT_RELEASED){
lv_obj_set_pos(calendar_obj,-600,5);
}

code of calendar "constructor"

 void lv_ex_calendar_2(lv_obj_t* global_calendar)
{
   lv_obj_t* lv_calendar_poi = lv_calendar_create(tab0, NULL);

    lv_obj_set_size(lv_calendar_poi, 500, 500);
    //lv_obj_align(calendar, NULL, LV_ALIGN_CENTER, 0, 0);
   lv_obj_set_pos(lv_calendar_poi,-600,10);
    lv_obj_set_event_cb(lv_calendar_poi, calendar_event_handler);

    /*Make the date number smaller to be sure they fit into their area*/
    lv_obj_set_style_local_text_font(lv_calendar_poi, LV_CALENDAR_PART_DATE, LV_STATE_DEFAULT, lv_theme_get_font_small());

    /*Set today's date*/
    lv_calendar_date_t today;
    today.year = 2021;
    today.month = 10;
    today.day = 23;

    lv_calendar_set_today_date(lv_calendar_poi, &today);
    lv_calendar_set_showed_date(lv_calendar_poi, &today);
    global_calendar=lv_calendar_poi;
//lv_obj_set_hidden(calendar,true);
    }

global variable of calendar pointer that i want to use.

volatile  lv_obj_t* calendar_obj=null;

init call

lv_ex_calendar_2(calendar_obj);

seems i am using version 7.1
and there is no method called lv_calendar_get_today_date

lv_calendar_date_t * lv_calendar_get_today_date ( const lv_obj_t * calendar )

so how could i get day of week?

I believe lv_calendar_get_today_date has existed since 5.3, so I’m not sure why it’s missing on your end. It is definitely in lv_calendar.h on the release/v7 branch.

sorry get_day_of_week
i exists in lv_calendar.c

static uint8_t get_day_of_week(uint32_t year, uint32_t month, uint32_t day)
{
    uint32_t a = month < 3 ? 1 : 0;
    uint32_t b = year - a;

#if LV_CALENDAR_WEEK_STARTS_MONDAY
    uint32_t day_of_week = (day + (31 * (month - 2 + 12 * a) / 12) + b + (b / 4) - (b / 100) + (b / 400) - 1) % 7;
#else
    uint32_t day_of_week = (day + (31 * (month - 2 + 12 * a) / 12) + b + (b / 4) - (b / 100) + (b / 400)) % 7;
#endif

    return day_of_week;
}


but its static without prototype. so by default i cant call it. so i need to copy implementation code or maybe clear static directive and add function prototype to lv_calendar.h

can u tell me how to properly make something like a constructor for component?
for example button with label and i working links to them (to work with button and label)

ok problem with constructors in C solved. ive used pointer to an object pointer

void create_checkable_btn(lv_obj_t ** btn,lv_obj_t ** label ,lv_obj_t * par,lv_style_t* style, lv_coord_t pos_x,lv_coord_t pos_y,lv_coord_t size_wide,lv_coord_t size_height,char btn_text[],lv_event_cb_t event_call_back_function)