错误“初始化元素不是常量"尝试使用 const 初始化变量时 [英] Error "initializer element is not constant" when trying to initialize variable with const

查看:57
本文介绍了错误“初始化元素不是常量"尝试使用 const 初始化变量时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下程序的第 6 行(将 my_foo 初始化为 foo_init)出现错误,我不确定我明白为什么.

I get an error on line 6 (initialize my_foo to foo_init) of the following program and I'm not sure I understand why.

typedef struct foo_t {
    int a, b, c;
} foo_t;

const foo_t foo_init = { 1, 2, 3 };
foo_t my_foo = foo_init;

int main()
{
    return 0;
}

请记住,这是我正在处理的一个更大的多文件项目的简化版本.目标是在目标文件中有一个常量,多个文件可以使用它来初始化状态结构.由于它是一个资源有限的嵌入式目标并且结构不是那么小,我不想要源的多个副本.我不想使用:

Keep in mind this is a simplified version of a larger, multi-file project I'm working on. The goal was to have a single constant in the object file, that multiple files could use to initialize a state structure. Since it's an embedded target with limited resources and the struct isn't that small, I don't want multiple copies of the source. I'd prefer not to use:

#define foo_init { 1, 2, 3 }

我也在尝试编写可移植的代码,所以我需要一个有效的 C89 或 C99 的解决方案.

I'm also trying to write portable code, so I need a solution that's valid C89 or C99.

这与目标文件中的 ORG 有关系吗?初始化的变量进入一个 ORG 并通过复制第二个 ORG 的内容来初始化?

Does this have to do with the ORGs in an object file? That initialized variables go into one ORG and are initialized by copying the contents of a second ORG?

也许我只需要改变我的策略,让一个初始化函数在启动时完成所有的副本.除非有其他想法?

Maybe I'll just need to change my tactic, and have an initializing function do all of the copies at startup. Unless there are other ideas out there?

推荐答案

在 C 语言中,具有静态存储期的对象必须使用常量表达式或包含常量表达式的聚合初始化器进行初始化.

In C language, objects with static storage duration have to be initialized with constant expressions, or with aggregate initializers containing constant expressions.

大"对象永远不是 C 中的常量表达式,即使该对象被声明为 const.

A "large" object is never a constant expression in C, even if the object is declared as const.

此外,在C语言中,术语常量"指的是文字常量(如1'a'0xFF 等)、枚举成员以及 sizeof 等运算符的结果.Const 限定的对象(任何类型)在 C 语言术语中不是常量.无论它们的类型如何,它们都不能用于具有静态存储期的对象的初始值设定项.

Moreover, in C language, the term "constant" refers to literal constants (like 1, 'a', 0xFF and so on), enum members, and results of such operators as sizeof. Const-qualified objects (of any type) are not constants in C language terminology. They cannot be used in initializers of objects with static storage duration, regardless of their type.

例如,这不是一个常量

const int N = 5; /* `N` is not a constant in C */

上面的 N 在 C++ 中是一个常量,但在 C 中不是一个常量.所以,如果你尝试做

The above N would be a constant in C++, but it is not a constant in C. So, if you try doing

static int j = N; /* ERROR */

你会得到同样的错误:尝试用非常量来初始化一个静态对象.

you will get the same error: an attempt to initialize a static object with a non-constant.

这就是为什么在 C 语言中,我们主要使用 #define 来声明命名常量,也使用 #define 来创建命名聚合初始化器的原因.

This is the reason why, in C language, we predominantly use #define to declare named constants, and also resort to #define to create named aggregate initializers.

这篇关于错误“初始化元素不是常量"尝试使用 const 初始化变量时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆