在C[复制]中初始化元素不是常数

时间:2021-12-14 16:07:20

Possible Duplicate:
Error “initializer element is not constant” when trying to initialize variable with const

可能的重复:当尝试使用const初始化变量时,错误“初始化元素不是常量”。

I'm coming from javascript/php/python and probably I'm missing something, here is the code:

我来自javascript/php/python,可能我漏掉了什么,这里是代码:

const int a = 50;
const int c = 100;
const int d = 100;
int endX = c + a;
int endY = d;
int startX, startY, b;

I get

我得到

ex1.4.c:6: error: initializer element is not constant
ex1.4.c:7: error: initializer element is not constant

ex1.4。c:6:错误:初始化元素不是常量ex1.4。c:7:错误:初始化元素不是常量。

Someone has an explanation?

有人一个解释吗?

5 个解决方案

#1


4  

If you are declaring endX as a global variable the error makes sense.

如果您将endX声明为全局变量,那么这个错误是有意义的。

The reason is that global variables are initialized in compiling time, and you are trying to initialize endX as an operation that must be done in execution time.

原因是在编译时,全局变量被初始化,并且您试图将endX初始化为在执行时必须执行的操作。

#2


15  

Unfortunately, in C const variables are not really const.

不幸的是,在C常量变量中并不是真正的常量。

Below are the extracts from c99 standard.

以下是c99标准的摘录。

6.7.8 Initialization

6.7.8初始化

  1. All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
  2. 对于具有静态存储时间的对象,初始化器中的所有表达式都应该是常量表达式或字符串常量。

The constants are defined as follows:

常数的定义如下:

6.4.4 Constants

6.4.4常量

Syntax

语法

constant:

不变:

integer-constant       (e.g. 4, 42L)
floating-constant      (e.g. 0.345, .7)
enumeration-constant   (stuff in enums)
character-constant     (e.g. 'c', '\0')

The standard defines constant expressions as follows:

标准定义常数表达式如下:

6.6 Constant expressions

6.6常数表达式

(7) More latitude is permitted for constant expressions in initializers. Such a constant expression shall be, or evaluate to, one of the following:

(7)在初始化器中允许对常量表达式进行更多的纬度。这样的常数表达式应该是,或计算为:

— an arithmetic constant expression,

-一个算术常数表达式,

— a null pointer constant,

-空指针常数,

— an address constant, or

-地址常数,或。

— an address constant for an object type plus or minus an integer constant expression.

-一个对象类型加或减一个整数常量表达式的地址常量。

(8) An arithmetic constant expression shall have arithmetic type and shall only have operands that are integer constants, floating constants, enumeration constants, character constants, and sizeof expressions. Cast operators in an arithmetic constant expression shall only convert arithmetic types to arithmetic types, except as part of an operand to a sizeof operator whose result is an integer constant.

(8)算术常量表达式应具有算术类型,且仅具有整数常量、浮点常量、枚举常量、字符常量和sizeof表达式的操作数。在算术常量表达式中,Cast运算符只将算术类型转换为算术类型,除非作为操作数的一部分,并使其结果为整数常量。

Thus, c and a are not constant expressions and cannot be used as initializers in your case.

因此,c和a不是常量表达式,不能作为初始化器使用。

#3


5  

const expressions must be a compile time constant in C unlike in C++ therefore c+a can't be used as a constant. The usual way to handle this problem in C is to use the preprocessor instead:

在C中,const表达式必须是一个编译时常量,因此C+ a不能作为常量使用。在C中处理这个问题的通常方法是使用预处理器:

#define A 50
#define C 100
#define D 100
int endX = C + A;
int endY = D;
int startX, startY, b;

#4


2  

Yeah, you can't initialize something to a variable. The compiler does the initialization and at compile time it doesn't know the value of c+a;

对,你不能对变量进行初始化。编译器进行初始化,在编译时它不知道c+a的值;

The int x = 1; type initialization is fine, the compiler just puts a 1 at the address of x in the object code.

int x = 1;类型初始化很好,编译器只在对象代码中的x地址中放置一个1。

To initialize something to c+a, you want to do it at runtime, in the startup code in c or constructor in C++.

要将某些东西初始化到c+a,您需要在运行时,在c或c++中的c或构造函数的启动代码中执行。

#5


0  

In C programming langages, objects with static storage duration must be initialized with constant expressions (or aggregate containing constant expressions). If endX has static storage duration, its initializer (c+a) is not a constant expression (i.e. the expression cannot be evaluated during translation phase).

在C编程语言中,静态存储持续时间的对象必须用常量表达式(或包含常量表达式的集合)初始化。如果endX具有静态存储时间,它的初始化器(c+a)不是一个常量表达式(即在翻译阶段不能计算表达式)。

#1


4  

If you are declaring endX as a global variable the error makes sense.

如果您将endX声明为全局变量,那么这个错误是有意义的。

The reason is that global variables are initialized in compiling time, and you are trying to initialize endX as an operation that must be done in execution time.

原因是在编译时,全局变量被初始化,并且您试图将endX初始化为在执行时必须执行的操作。

#2


15  

Unfortunately, in C const variables are not really const.

不幸的是,在C常量变量中并不是真正的常量。

Below are the extracts from c99 standard.

以下是c99标准的摘录。

6.7.8 Initialization

6.7.8初始化

  1. All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
  2. 对于具有静态存储时间的对象,初始化器中的所有表达式都应该是常量表达式或字符串常量。

The constants are defined as follows:

常数的定义如下:

6.4.4 Constants

6.4.4常量

Syntax

语法

constant:

不变:

integer-constant       (e.g. 4, 42L)
floating-constant      (e.g. 0.345, .7)
enumeration-constant   (stuff in enums)
character-constant     (e.g. 'c', '\0')

The standard defines constant expressions as follows:

标准定义常数表达式如下:

6.6 Constant expressions

6.6常数表达式

(7) More latitude is permitted for constant expressions in initializers. Such a constant expression shall be, or evaluate to, one of the following:

(7)在初始化器中允许对常量表达式进行更多的纬度。这样的常数表达式应该是,或计算为:

— an arithmetic constant expression,

-一个算术常数表达式,

— a null pointer constant,

-空指针常数,

— an address constant, or

-地址常数,或。

— an address constant for an object type plus or minus an integer constant expression.

-一个对象类型加或减一个整数常量表达式的地址常量。

(8) An arithmetic constant expression shall have arithmetic type and shall only have operands that are integer constants, floating constants, enumeration constants, character constants, and sizeof expressions. Cast operators in an arithmetic constant expression shall only convert arithmetic types to arithmetic types, except as part of an operand to a sizeof operator whose result is an integer constant.

(8)算术常量表达式应具有算术类型,且仅具有整数常量、浮点常量、枚举常量、字符常量和sizeof表达式的操作数。在算术常量表达式中,Cast运算符只将算术类型转换为算术类型,除非作为操作数的一部分,并使其结果为整数常量。

Thus, c and a are not constant expressions and cannot be used as initializers in your case.

因此,c和a不是常量表达式,不能作为初始化器使用。

#3


5  

const expressions must be a compile time constant in C unlike in C++ therefore c+a can't be used as a constant. The usual way to handle this problem in C is to use the preprocessor instead:

在C中,const表达式必须是一个编译时常量,因此C+ a不能作为常量使用。在C中处理这个问题的通常方法是使用预处理器:

#define A 50
#define C 100
#define D 100
int endX = C + A;
int endY = D;
int startX, startY, b;

#4


2  

Yeah, you can't initialize something to a variable. The compiler does the initialization and at compile time it doesn't know the value of c+a;

对,你不能对变量进行初始化。编译器进行初始化,在编译时它不知道c+a的值;

The int x = 1; type initialization is fine, the compiler just puts a 1 at the address of x in the object code.

int x = 1;类型初始化很好,编译器只在对象代码中的x地址中放置一个1。

To initialize something to c+a, you want to do it at runtime, in the startup code in c or constructor in C++.

要将某些东西初始化到c+a,您需要在运行时,在c或c++中的c或构造函数的启动代码中执行。

#5


0  

In C programming langages, objects with static storage duration must be initialized with constant expressions (or aggregate containing constant expressions). If endX has static storage duration, its initializer (c+a) is not a constant expression (i.e. the expression cannot be evaluated during translation phase).

在C编程语言中,静态存储持续时间的对象必须用常量表达式(或包含常量表达式的集合)初始化。如果endX具有静态存储时间,它的初始化器(c+a)不是一个常量表达式(即在翻译阶段不能计算表达式)。