c - 声明结构指针时出错

标签 c gcc avr avr-gcc

我试图在我的微 Controller 上实现一个配置读取器和写入器(使用 AVR-GCC),但遇到了一些编译错误,特别是:

error: expected constructor, destructor, or type conversion before '(' token
error: expected constructor, destructor, or type conversion before '.' token

在我的主文件中。造成这种情况的代码是:

#include "cfg.h"

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

config_read(config);

cfg.h 的内容:

#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));
}

最佳答案

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);

关于c - 声明结构指针时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24732579/

相关文章:

c - sprintf() 是否跨函数修改变量?

c++ - 在 GCC 中链接 .a 和 .o 文件

c++ - GCC 模板推导消除 const 错误?

c - 与GCC中的间接访问相比,为什么直接访问结构成员会产生更多的汇编代码?

c - ATMega 随机数生成

c - c中fork()&&fork()||fork()是什么意思

c - 如何在 c 中使用 openssl 创建聊天服务器

c - 如何停止循环并等待从串行接收到不同的值

c++ - 在 winavr 中需要帮助

objective-c - Objective-C : Resource busy 中的串行通信错误