2014-07-14 112 views
1

我想實現我的微控制器(使用AVR-GCC)一個配置讀者與作者,和我遇到了一些編譯錯誤,特別是。造成這種代碼是:的cfg.h錯誤與聲明結構指針

#include "cfg.h" 

struct config_t* config; 
config.temp = TEMP_C; 
config.precision = 9; 
config.time = 0; 

config_read(config); 

內容:

#ifndef CFG_h 
#define CFG_h 

#include <avr/eeprom.h> 
#include <inttypes.h> 

#define TEMP_C 0 
#define TEMP_F 1 
#define TEMP_K 2 

typedef struct config_t { 
    uint8_t temp; 
    uint8_t precision; 
    int32_t time; 
} config_t; 

void config_read(struct config_t * config); 
void config_write(const struct config_t * config); 

#endif 

和內容cfg.c

#include "cfg.h" 

void config_read(struct config_t* config) { 
    eeprom_read_block((void*)&config, (void*)(0), sizeof(config_t)); 
} 

void config_write(const struct config_t* config) { 
    eeprom_write_block((const void*)&config, (void*)(0), sizeof(config_t)); 
} 
+0

D'oh!原來,我以爲我已經把這個代碼放在主要方法中,但我沒有。儘管如此,它仍然是一個非常神祕的錯 – svbnet

+0

這個問題的所有答案指出了不同的錯誤,但它們都是正確的 – jforberg

回答

1

由於您使用的是struct config_t*,你應該使用箭頭代替當初始化您的config變量時出現點。

+0

現在錯誤消息已經改變爲'錯誤:期望的構造函數,析構函數或類型轉換' - >'token'之前。我也嘗試過'struct config_t * config =(config_t *)malloc(sizeof(config_t));'但沒有區別。 – svbnet

+0

您確定這是導致錯誤消息的代碼的這一部分嗎?因爲我沒有看到這4條線有什麼問題。您應該在下面嘗試@MattMcNabb答案,以便您不必使用指針。 – k94ll13nn3

2
struct config_t* config; 

問題:config是未初始化的變量。你需要使用malloc來創建一個對象config_t結構

config.temp = TEMP_C; 
config.precision = 9; 
config.time = 0; 

問題:由於config_t是一個指針,您可以訪問使用config_t結構的方法或變量「 - >」操作不操作「」

像:

config->temp = TEMP_C; 
config->precision = 9; 
config->time = 0; 

config_read(config); 
1

config_read功能是閱讀配置。設置config的成員並調用它是沒有意義的。相反,這樣做:

struct config_t config; 
config_read(&config); 

寫配置時,這將是有意義的設置變量:

struct config_t config = { 0 }; 
config.temp = TEMP_C; 
config.precision = 9; 
config.time = 0; 
config_write(&config); 
1

當您使用的typedef即

typedef struct config_t { 
    uint8_t temp; 
    uint8_t precision; 
    int32_t time; 
} config_t; 

它不是必要的,你總是使用struct while using the structure,and when u are using pointer to access structure members you must use using「config_t-> member of structure」而不是config_t.m結構的餘燼