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

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

问题描述

我得到了下面的程序第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;
}

请,这是我工作的一个更大的,多文件项目的简化版本。目标是具有在对象文件中的单个恒定,多个文件可以用它来初始化的状态下的结构。因为它是资源有限的嵌入式目标和结构是不小,我不想源的多个副本。我想preFER不使用:

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 }

我也想编写可移植的code,所以我需要一个解决方案,是有效的C89或C99。

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

这是否有在目标文件中的[机构呢?这初始化的变量去到一个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具有静态存储时间语言对象必须与含常量前的恒前pressions 的或聚合初始化初始化pressions。

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

一个大的对象是从来没有在C恒前pression,即使对象被声明为常量

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

此外,在C语言中的术语不变是指的字面常量的(如 1 'A' 0xFF的等)和枚举成员。 const限定的对象(任何类型)是的不是常量的C语言的术语。他们不能在静态存储持续时间对象的初始化可以使用,不管它们的类型。

Moreover, in C language the term "constant" refers to literal constants (like 1, 'a', 0xFF and so on) and enum members. 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语言中,我们predominantly使用的#define 来声明命名常量,也诉诸的#define <原因/ code>以创建命名总初始化。

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.

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

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