2015-02-07 58 views
0

qq4all。 我有個任務 - 寫配置解析器這樣的語法:無法將數據寫入結構

[module] 
name = first 
imitationType = first 

[module] 
name = second 
imitationType = second 

等 我發現相當配置解析器 - inih,但我不能強迫它的工作,因爲我想要的。在這裏我的代碼,所著超過inih例如:

typedef struct { 
    const char* name; 
    const char* imitation_type; 
} module_config; 

int module_count = 0; 

static int handler(void* user, const char* section, const char* name, 
        const char* value) 
{ 
    module_config* pconfig = (module_config*)user; 
    pconfig = (module_config *) malloc(module_count*sizeof(module_config)); 
    #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0 
    if (strcmp(section, "module") == 0) { 
     if (MATCH("module", "name")) { 
      pconfig[module_count]->version = strdup(value); 
     } else if (MATCH("module", "imitationType")) { 
      pconfig[module_count]->name = strdup(value); 
     } else { 
      return 0; /* unknown section/name, error */ 
     } 
    ++module_count; 
    pconfig = (module_config *) realloc(pconfig, module_count * sizeof(module_config)); 
} 

return 1; 
} 

但是,當我嘗試編譯,我得到一個錯誤:

Error! Expression for '->' must be 'pointer to struct or union'

那些行:

pconfig[module_count]->version = strdup(value); 
pconfig[module_count]->name = strdup(value); 

我新手在編程,並不明白,爲什麼會發生這種情況。請幫助:-)

回答

1

pconfig是一個指向module_config結構實例的指針。

在這個指針上使用pconfig [module_count]相當於取消引用指針(pconfig+module_count),即它相當於*(pconfig+module_count)

因此,pconfig[module_count]不再是指針。您需要使用pconfig[module_count].version(pconfig+module_count)->version