【发布时间】:2018-10-24 22:15:26
【问题描述】:
我正在尝试创建一个 const 结构数组,但我不断得到
error initializer element is not a compile time constant
我正在使用 keil IDE。 这很奇怪,因为我的结构是一个常量,这里是一个例子:
typedef const struct{
CRGB color; // CRGB is another struct
void (*myfunc)(int);
}myProfile;
myProfile profile1 = { ....... }; // initialized struct
myProfile profiles[1] = { profile1 }; // error occurs here
即使我使用
const myProfile profile1 = { ..... };
初始化结构,我仍然得到同样的错误。
我可以找到解决方法,但我真的很想了解发生了什么。谢谢。
【问题讨论】:
-
初始值设定项必须是一个常量表达式,这是一个比具有
const条件的变量更强的条件。事实上,变量从来都不是常量表达式。 -
你能给我一个例子来说明你的意思吗@M.M 我可以做这样的事情
#define profile1 (myProfile){.....}我还有什么其他选择? -
在 typedef 中使用
const时要小心(你比我在那儿使用它更勇敢)。您可以使用myProfile *profiles[1] = &profile1;,用另一个配置文件的常量地址初始化数组中的指针。这可能会给您带来可以接受的结果;您可以在指针中添加const限定符。 -
谢谢乔纳森,你是对的。但在这种情况下,配置文件无论如何都应该保持不变。