为什么在分配类型'uint16_t'错误时会出现不兼容的类型?

nda*_*ess 1 c incompatibletypeerror

incompatible types when assigning to type 'uint16_t {aka short unsigned int}' from type 'ble_uuid_t {aka struct <anonymous>}在这段代码中得到了一个

ble_uuid_t    ble_uuid;

ble_uuid.uuid = m_adv_uuid[0];
Run Code Online (Sandbox Code Playgroud)

在哪里我定义m_adv_uuid

#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};
Run Code Online (Sandbox Code Playgroud)

ble_uuid_t定义为

typedef struct
{
  uint16_t    uuid; 
  uint8_t     type; 
} ble_uuid_t;
Run Code Online (Sandbox Code Playgroud)

提前致谢

vas*_*sek 5

实际上有两个问题:

  1. 你正在初始化结构数组,必须这样做:

    ble_uuid_t m_adv_uuid[] = { { AMT_SERVICE_UUID, AMTS_CHAR_UUID } };
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后你正在访问struct而不是它的成员uuid.您的作业应如下所示:

    ble_uuid.uuid = m_adv_uuid[0].uuid;
    
    Run Code Online (Sandbox Code Playgroud)

或者,当然,如果你不想m_adv_uuid成为一个数组ble_uuid_t,只需删除你的代码的[]声明m_adv_uuid和其余代码就可以了:

ble_uuid_t m_adv_uuid = {AMT_SERVICE_UUID, AMTS_CHAR_UUID};
Run Code Online (Sandbox Code Playgroud)