C初始化元素不是常量 - 结构中的指针

时间:2015-11-16 03:46:53

标签: c pointers struct initializer

代码段显示在

下方
/// System Parameters for the gateway
typedef struct {
    bool        mobile_gateway;         // true = GPS receiver used, else false
    uint16_t    gw_ordinal_number;      // Starting at 1, the ordinal gateway number
} CONFIG_SYSTEM_PARMS;


// Data types for the system parameters
typedef enum {
    CFGPARM_TYPE_BOOL,
    CFGPARM_TYPE_UINT,
    CFGPARM_TYPE_INT,
    CFGPARM_TYPE_DOUBLE,

    CFGPARM_NUM_TYPES
} CONFIG_PARM_DATA_TYPES;    


// These mnemonic names and data types correspond to the system parameters
#define SYS_PARM_MOBILE_GATEWAY_MNEM    "MOBILEGW"
#define SYS_PARM_GATEWAY_NUMBER_MNEM    "GWNUM"
#define SYS_PARM_MOBILE_GATEWAY_TYPE    CFGPARM_TYPE_BOOL
#define SYS_PARM_GATEWAY_NUMBER_TYPE    CFGPARM_TYPE_UINT


typedef struct {
    char        FILLER;         // TODO - Define User Parms later...
} CONFIG_USER_PARMS;             

static const uint16_t TOTAL_CONFIG_PARM_BYTESIZE = sizeof(CONFIG_SYSTEM_PARMS) + sizeof(CONFIG_USER_PARMS);

typedef struct {
    const char              *sysparm_mnemonic;
    CONFIG_PARM_DATA_TYPES  sysparm_data_type;
    void                    *sysparm_data_ptr;
} CONFIG_PARM_LOOKUP_TABLE;    

// ****************************************

static LT_LOGGER_DATA  gateway_log_data;
static CONFIG_SYSTEM_PARMS *config_sys_parms_ptr = &gateway_log_data.config_system_parms;

// **************************************************
// Lookup table for the System Parms
// 
// CAA: This is a stinking mess - There is no easy way to define &gateway_log_data.config_system_parms
// outside of the following initialization, so the result is more verbose than it should be
static const CONFIG_PARM_LOOKUP_TABLE config_parm_lookup_table[] = {
    { SYS_PARM_MOBILE_GATEWAY_MNEM,     SYS_PARM_MOBILE_GATEWAY_TYPE,   &config_sys_parms_ptr->mobile_gateway},
    { SYS_PARM_GATEWAY_NUMBER_MNEM,     SYS_PARM_GATEWAY_NUMBER_TYPE,   &gateway_log_data.config_system_parms.gw_ordinal_number},

    // FILLER to mark end of list
    { "",                               0,                              NULL} 
};

问题是: 为什么,我得到错误:初始化元素不是常量(接近初始化' config_parm_lookup_table [0] .sysparm_data_ptr') 当config_parm_lookup_table [1] .sysparm_data_ptr的初始化器有效地使用相同的构造时?

显然,我有一个解决办法,但似乎很冗长。

0 个答案:

没有答案