pkuzhx 发表于 2017-3-8 15:52:34

这个赋值如何解释?

在BLE client中有一个赋值,有些看不明白。
SERVICE_FUNC_POINTERS_T BatteryServiceFuncStore = {
    .serviceUuid            = &BatteryServiceUuid,
    .isMandatory            = NULL,
    .serviceInit            = &BatteryServiceDataInit,
    .checkHandle            = &BatteryServiceCheckHandle,
    .getHandles             = &BatteryServiceGetHandles,
    .charDiscovered         = &BatteryServiceCharDiscovered,
    .descDiscovered         = &BatteryServiceCharDescDisc,
    .discoveryComplete      = &BatteryServiceDiscoveryComplete,
    .serviceIndNotifHandler = &BatteryServiceHandlerNotifInd,
    .configureServiceNotif= &BatteryServiceConfigNotif,
    .writeRequest         = NULL,
    .writeConfirm         = &BatteryServiceWriteConfirm,
    .readRequest            = &BatteryServiceReadRequest,
    .readConfirm            = &BatteryServiceReadConfirm,
    .configureService       = &BatteryServiceConfigure,
    .isServiceFound         = &BatteryServiceFound,
    .resetServiceData       = &BatteryServiceResetData
};该数据类型定义如下,是一个结构体,成员都是函数指针:
/* Service callbacks */
typedef struct _SERVICE_FUNC_POINTERS_T
{
    /* Returns the service UUID and its type (16-bit or 128-bit) */
    void (*serviceUuid)(GATT_UUID_T *type, uint16 *uuid);

    /* Returns TRUE if the service is mandatory */
    bool (*isMandatory)(void);

    /* Initialise service data */
    void (*serviceInit)(uint16 dev_num,
                        GATT_DISC_PRIM_SERV_BY_UUID_IND_T *p_event_data);

    /* Returns TRUE if the handle is supported by the service */
    bool (*checkHandle)(uint16 dev_num, uint16 handle);
   
    /* Get the start and end handle for the service or characteristic */
    bool (*getHandles)(uint16 dev_num,
                     uint16 *StartHandle,
                     uint16 *EndHandle,
                     gatt_profile_hierarchy_t type);

    /* Called for each discovered characteristic */
    bool (*charDiscovered)(uint16 dev_num,
                           GATT_CHAR_DECL_INFO_IND_T *p_event_data);

    /* Called for each discovered characteristic descriptor. This function must
   * only be called after first calling charDiscovered.
   */
    void (*descDiscovered)(uint16 dev_num,
                           GATT_CHAR_DESC_INFO_IND_T *p_event_data);

    /* Called when the Discovery Procedure is complete for the current server.
   * Returns TRUE if the callback starts a read or write, otherwise FALSE
       */
    bool (*discoveryComplete)(uint16 dev_num,
                              uint16 connect_handle);

    /* Configure notifications for a given characteristic by writing to the
   * Client Characteristic Configuration Descriptor. Returns TRUE, if
   * successful, otherwise FALSE
   */
    bool (*configureServiceNotif)(uint16 dev_num,
                                  uint16 type,
                                  uint8 sub_type,
                                  bool enable);

    /* Callback for handling indications and notifications */
    bool (*serviceIndNotifHandler)(uint16 dev_num,
                                 uint16 handle,
                                 uint16 size,
                                 uint8 *value);

    /* Callback to modify the value of a characteristic on the peer device */
    bool (*writeRequest)(uint16 dev_num,
                         uint16 type,
                         uint8 *data,
                         uint16 size);

    /* Function called after a characteristic value has been successfully
   * written
    */
    void (*writeConfirm)(uint16 dev_num,
                         uint16 connect_handle);

    /* Callback to read the value of a characteristic on the peer device */
    bool (*readRequest)(uint16 dev_num, uint16 type);

    /* Function called when a characteristic value has been successfully read */
    void (*readConfirm)(uint16 dev_num,
                        uint16 size,
                        uint8 *value);

    /* Configure the service to request notifications or indications */
    bool (*configureService)(uint16 dev_num);

    /* Check if the service is present on the specified device */
    bool (*isServiceFound)(uint16 dev_num);
   
    /* Reset the service data */
    void (*resetServiceData)(uint16 dev_num);
} SERVICE_FUNC_POINTERS_T;结构体赋初值不应该是如下这样吗?
SERVICE_FUNC_POINTERS_T BatteryServiceFuncStore = {
       &BatteryServiceUuid,
       NULL,
       &BatteryServiceDataInit,
       &BatteryServiceCheckHandle,
       &BatteryServiceGetHandles,
       &BatteryServiceCharDiscovered,
       &BatteryServiceCharDescDisc,
       &BatteryServiceDiscoveryComplete,
       &BatteryServiceHandlerNotifInd,
       &BatteryServiceConfigNotif,
       NULL,
       &BatteryServiceWriteConfirm,
       &BatteryServiceReadRequest,
       &BatteryServiceReadConfirm,
       &BatteryServiceConfigure,
       &BatteryServiceFound,
       &BatteryServiceResetData
};例程中,成员前加一个.然后再=,这种赋值法在C里边没有见过啊,而且放到标准的C编译器里也编译不过。是SDK支持的特殊语法吗?

pkuzhx 发表于 2017-3-8 15:57:42

我最后的那种方法在SDK中也可以编译过,但是必须注意顺序不能乱。
第一个初始化的方法,成员顺序乱了也可以。但是这样赋值还是第一次见。

shenada 发表于 2017-3-13 17:33:56

GCC 專屬語法
請參照 6.27 Designated Initializers 章節
https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html

這種語法能避免賦值時意外將順序弄錯, 該語法只能用來初始結構變數用

pkuzhx 发表于 2017-3-13 18:05:29

shenada 发表于 2017-3-13 17:33
GCC 專屬語法
請參照 6.27 Designated Initializers 章節
https://gcc.gnu.org/onlinedocs/gcc/Designate ...

给力!多谢。
在VC里编译不通过,果然在单片机编译器里就可以通过了。
页: [1]
查看完整版本: 这个赋值如何解释?