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
  • The point of this declaration is that the array is actually located here, and it's not just a pointer to another area. In C99, you would write this as data[], but this is not legal in C89, hence this [1].

    Having it like this

    
    struct
    {
        uint8_t * data;
    } ble_gatts_evt_write_t;
    
    

    would imply there is just a pointer to some other memory area in the actual event structure, while this

    
    struct
    {
        uint8_t data[]; // or data[1] in C89
    } ble_gatts_evt_write_t;
    
    

    shows that the entire data is actually located in this memory area.

    Initially, it was a requirement for the softdevice headers to be C89 compliant, and even though we've abandoned this, some things like this still lingers in the code. However, when using this buffer, these two declarations are mostly equivalent, although it may matter in case you need to copy the structure.

    It seems to me that this blog post explains the difference between a pointer and an array in a good way, if the difference is still unclear.

  • I'm wasn't offended, but I find it strange to downvote an answer, not because it's wrong, but because you personally disagree with it. Even when re-reading your original question and my answer, I honestly think I've answered the exact questions you were raising.

    Since I don't know a lot about your proficiency with C, I found it appropriate to include some pointers for further information in case my explanation wasn't clear. This may also be useful for other people that later may find this question.

    I do agree with your general intentions of writing clear code, it's just that we disagree on whether this particular definition is clear or not. Using a well-established pattern, even though an apparently slightly controversial one, is normally exactly what I'd say is a good solution.

    Warnings should of course always be avoided, and that something works is in itself no excuse for having out-of-spec code, it's just that this snippet in question is not out-of-spec.

    I'm not going to go further into your try to make this a discussion on education and profession, but I can assure you that most people working in our software department are properly educated software engineers.

Reply
  • I'm wasn't offended, but I find it strange to downvote an answer, not because it's wrong, but because you personally disagree with it. Even when re-reading your original question and my answer, I honestly think I've answered the exact questions you were raising.

    Since I don't know a lot about your proficiency with C, I found it appropriate to include some pointers for further information in case my explanation wasn't clear. This may also be useful for other people that later may find this question.

    I do agree with your general intentions of writing clear code, it's just that we disagree on whether this particular definition is clear or not. Using a well-established pattern, even though an apparently slightly controversial one, is normally exactly what I'd say is a good solution.

    Warnings should of course always be avoided, and that something works is in itself no excuse for having out-of-spec code, it's just that this snippet in question is not out-of-spec.

    I'm not going to go further into your try to make this a discussion on education and profession, but I can assure you that most people working in our software department are properly educated software engineers.

Children
No Data
Related