error C2099: initializer is not a constant

in file main.c there is a code.
here is the code:

The initialization value must be a compile time constant. a is a
variable. Even if you defined a as const short, it is still not a
compile time constant.
解决方案

Your options for compile time constant initialization goes beyond hard-coded
values: You may initialize it to some macro's value (#define BOOOO_HISSSS)
or a specific enum value. The enum hackonly works for int.

With variables you can of course get around it by simply assigning after
declaring(instead of all-in-one initialization). The real problem rears it's
ugly head when you want to inizialize a const with somevalue based another
const.

Are there any other ways besides setting them to a macro or enum?

i.e.

------------------msdn上的样例和解释---------------------

C2099 can also occur because the compiler is not able to perform constant folding on an expression under /fp:strict because the floating point precision environment settings (see _controlfp_s for more information) may differ from compile to run time.

When constant folding fails, the compiler invokes dynamic initialization, which is not allowed in C.

To resolve this error, compile the module as a .cpp file or simplify the expression.

For more information, see /fp (Specify Floating-Point Behavior).

或者

This error is issued only by the C compiler and occurs only for non-automatic variables. The compiler initializes non-automatic variables at the start of the program and the values they are initialized with must be constant.

或者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 错误:初始化元素不是常量。 这个错误通常出现在C语言中,表示在初始化一个全局变量或静态变量时,使用了一个不是常量的表达式。在C语言中,全局变量和静态变量必须在编译时就确定其初始值,而不能在运行时再确定。 解决方法是将初始化表达式改为常量表达式,或者将变量定义为局部变量,这样可以在运行时确定其初始值。 ### 回答2: “initializer element is not constant”是一种编译错误,这种错误通常在编译器遇到不能被预先计算的变量时出现,例如定义一个非常量的变量作为数组的初始值或静态存储的初始值。 详细来讲,对于C语言或C++,该错误通常出现在变量初始化的时候,例如: ```c++ int size = 10; int arr1[size]; // 错误:initializer element is not constant const int size2 = 10; int arr2[size2]; // 正确:size2是一个常量,编译器可以预先计算 ``` 在第一个例子中,变量`size`是一个非常量,其值只能在运行时确定,而不是编译时确定。因此,编译器无法在编译时预先计算数组的大小,从而导致错误。在第二个例子中,变量`size2`是一个常量,因此编译器可以在编译时预先计算数组的大小,因此没有错误。 类似地,在定义静态变量时,也会出现该错误。静态变量必须在编译时就具有初始值,例如: ```c++ static int value1 = 10; // 正确 static int value2 = size; // 错误:initializer element is not constant ``` 在第一个例子中,静态变量`value1`具有常量初始值,因此编译器可以在编译时计算它的初始值。在第二个例子中,变量`size`是一个非常量,编译器无法在编译时确定它的值,因此导致错误。 最后需要注意的是,如果你使用的是C++11或更新的版本,并且编译器支持变长数组,你可以通过`std::vector`或类似的容器来避免通过变量初始化静态数组的问题。例如: ```c++ std::vector<int> arr3(size); // C++11中支持变长数组 ``` 总之,“initializer element is not constant”常常因为在编译期无法确定变量的值而出现。如果想要避免这种错误,我们应该尽可能地使用常量来初始化变量或者使用C++11中支持的变长数组来解决这个问题。 ### 回答3: 在程序代码中常会遇到 error: initializer element is not constant 的错误提示。这一提示通常是由于在定义变量时,使用了一个不是常量的表达式作为初始值导致的。 定义变量时,需要指定一个初始值,如果这个初始值不是一个常量,就会出现上述的错误提示。这是因为在变量定义时,编译器需要为变量分配空间,并将初始值存储到该空间内。而由于这个初始值不是常量,因此编译器无法确定它的值是否会改变,也就无法为变量分配确定的空间。 解决这一问题的方法是使用常量作为变量的初始值。常量是指在程序运行期间不会改变的值,例如字面量(如整数、浮点数、字符串等)或者用 const 关键字修饰的变量。这样,在编译时,编译器就能确定变量初始值的值,并为变量分配相应的空间。 需要注意的是,当定义的变量是静态变量或全局变量时,必须使用常量作为初始值,并确保该初始值在编译时已经确定。而对于自动变量和动态分配的内存空间,初始值可以是一个变量,只要该变量的值在程序运行期间不会改变即可。 综上所述,error: initializer element is not constant 错误提示通常是由于使用了非常量作为变量初始值导致的。解决方法是使用常量作为变量初始值,以确保编译器能够为变量分配正确的空间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值