【发布时间】:2023-03-12 11:02:01
【问题描述】:
我在 Keil 中使用 C99 对微处理器进行编程,但在使用结构定义静态常量时遇到了困难。 以下编译没有问题:
static uint8_t const addr1 = 0x76;
static uint8_t const addr = addr1;
但以下不是:
typedef struct
{
uint8_t const address;
} bmp280_t;
static bmp280_t bmp280_0 = {
.address = 0x76,
};
static uint8_t const addr2 = bmp280_0.address;
最后一行导致编译错误:
......\main.c(97): error: #28: expression must have a constant value static uint8_t const addr2 = bmp280_0.address;
我尝试在 Visual Studio 中进行复制,但两种情况都无法编译。我不知道这是因为它是编译为 cpp 还是使用不同的标准..
【问题讨论】:
-
如果第一个编译得很好,那么它使用了编译器扩展。常量变量不能用于标准 C 中的初始化。
-
好吧,我想可能是这样的。谢谢。