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

Confusing definition

C:\Nordic Semiconductor\nrf51_sdk_v4_4_1_31827\nrf51822\Include\ble\softdevice\ble_gatts.h


typedef struct {
  uint16_t     handle;    /**< Attribute Handle. */
  uint8_t       op;          /**< Type of write operation, see @ref BLE_GATTS_OPS. */
  ble_gatts_attr_context_t    context;     /**< Attribute Context. */
  uint16_t      offset;      /**< Offset for the write operation. */
  uint16_t      len;          /**< Length of the incoming data. */
  uint8_t       data[1];    /**< Incoming data, variable length. */
} ble_gatts_evt_write_t;

My question is how to interpret the element data[1]? Is it just uint8_t *data(C-wise it is)? If so why is it defined in such a way? Especially with the size of the data array as one with the following comment about its variable length which all together makes this definition very confusing.

Thanks.

Parents
  • One more thing: if array has predefined length then it needs to be less than uint16_t len; /* Length of the incoming data. */.

    The way I see the intention of the developer is:

    typedef union {
    unit8_t event_x[1];  /* event x message */
    uint16_t event_y[2];/* event y message */
    void *event_z; /* event z message */
    /* more event messages if necessary */
    } event_message_t; /* union will have the size of known largest message */
    struct
    {
    ...
    uint16_t len; /* Length of the incoming data; len <= sizeof(event_message_t); */
    event_message_t data;
    }ble_gatts_evt_write_t;
    
    
  • Beware that you are never expected to initialize a struct of this type, as it's only an event structure, initialized by the softdevice to notify you as an application of an incoming Write Command / Request.

    If you are to initialize some other struct employing the same concept, you would manually have to make sure to initialize it with appropriate size for the data you want to fit.

    After checking a little more, it's apparently known as the "struct hack", and explained in more detail here, including how to initialize such struct: http://c-faq.com/struct/structhack.html

Reply Children
No Data
Related