This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Array initialization in bsp.c

In the file bsp.c (from the SDK11 ble_hrs example) I don't quite understand the initialization statement:

static bsp_button_event_cfg_t m_events_list[BUTTONS_NUMBER] = {{BSP_EVENT_NOTHING, BSP_EVENT_NOTHING}};

bsp_button_event_cfg_t is a struct with three fields, so what does this initialization do?

Parents
  • Admittedly I had to compile this to double check, but...

    But if you compile that code under gcc it initializes the first element of the array (which is a struct bsp_button_event_cfg_t )

    With values

    push_event=BSP_EVENT_NOTHING;
    long_push_event=BSP_EVENT_NOTHING;
    

    The other property of the struct ( release_event ) does not get initialized, and on gcc its value is zero (which is the same as the enum for BSP_EVENT_NOTHING, but thats partially coincidental.

    Also only the first element of the array gets initialized; which is a bit of a concern, considering BUTTONS_NUMBER is defined in the header file for the board in question, and seems to have possible values of 0 2,3,4 or 8 depending on which board you have.

    I think the only reason this code actually works, is that the enum for BSP_EVENT_NOTHING is defined as 0, and the C compiler initializes static variables to zero which is the same as BSP_EVENT_NOTHING.

    e.g. If you had a board that had 8 buttons, and BSP_EVENT_NOTHING was defined to 1, instead of zero, it would most likely not work.

    I think the code should really be updated to remove the initialization, which is misleading and incorrect for any board that has more than 1 button.

    static bsp_button_event_cfg_t m_events_list[3];
    
  • Thanks Roger. I also think that this "partial" initialization is misleading since the array gets set with the correct values during bsp_init().

Reply Children
No Data
Related