Search code examples
cincompatibletypeerror

Why do I get an incompatible types when assigning to type 'uint16_t ' error?


I am getting a incompatible types when assigning to type 'uint16_t {aka short unsigned int}' from type 'ble_uuid_t {aka struct <anonymous>} in this piece of code

ble_uuid_t    ble_uuid;

ble_uuid.uuid = m_adv_uuid[0];

where I have defined m_adv_uuid as

#define AMT_SERVICE_UUID             0x2001
#define AMTS_CHAR_UUID               0x20
#define AMT_RCV_BYTES_CNT_CHAR_UUID  0x2003

ble_uuid_t m_adv_uuid[] = {AMT_SERVICE_UUID, AMTS_CHAR_UUID};

being ble_uuid_t defined as

typedef struct
{
  uint16_t    uuid; 
  uint8_t     type; 
} ble_uuid_t;

Thanks in advance


Solution

  • There are actually two problems:

    1. you are initializing array of structs, which must be done like this:

      ble_uuid_t m_adv_uuid[] = { { AMT_SERVICE_UUID, AMTS_CHAR_UUID } };
      
    2. and then you are accessing struct instead of its member uuid. Your assignment should look like this:

      ble_uuid.uuid = m_adv_uuid[0].uuid;
      

    Or, of course, if you don't want m_adv_uuid to be an array of ble_uuid_t, it's enough to just remove [] in declaration of m_adv_uuid and rest of your code will work:

    ble_uuid_t m_adv_uuid = {AMT_SERVICE_UUID, AMTS_CHAR_UUID};