25:警告:初始元素不是恒定的前pression [英] 25: warning: initializer element is not a constant expression

查看:192
本文介绍了25:警告:初始元素不是恒定的前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC让我在编译的时候下面的警告消息:

GCC gives me the following warning message when trying to compile:

las.c:13:18: warning: initializer element is not a constant expression [enabled by default]
 const int ROWS = pow (2, MESH_K);

此相关的code部分是:

The relevant code portions for this is:

 #define MESH_K 10
 #define BUFF_SIZE 30

 const int ROWS = pow (2, MESH_K);

我需要在code。在后点同时使用MESH_K和行。据我所知,函数调用可能导致GCC认为这不是一个恒定的前pression。然而鉴于此调用战俘是基本恒定的,有没有实现它(pre-处理器宏吧?),并消除警告更好的办法?

I need to use both MESH_K and ROWS at later points in the code. I understand that function calls are probably leading GCC to believe that this is not a constant expression. However given that this call to pow is essentially a constant, is there a better way to implement it (pre-processor macros perhaps?) and eliminate the warning?

我不介意牺牲性能可读性于code的一部分,所以任何和所有复杂的解决方案是受欢迎的。

I don't mind sacrificing readability for performance in this part of the code, so any and all complex solutions are welcome.

推荐答案

我相信你的答案是<一个href=\"http://stackoverflow.com/questions/3025050/error-initializer-element-is-not-constant-when-trying-to-initialize-variable-w\">here.

这将编译用C ++罚款,但不是在C

This will compile fine in C++, but not in C.

它与C语言做的。在C语言中使用静态对象
  存储时间已与恒前pressions或初始化
  含常量前pressions总初始化。

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

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

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

此外,在C语言中的术语不变是指字面
  常数(象1,'一个',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的行数= 1&LT;&LT; MESH_K; 将工作,但:

Like squeamish says, const int ROWS = 1 << MESH_K; will work, but:

int test = 10;

const int ROWS = 1 << test;

将无法正常工作。我的猜测是, MESH_K 粘贴到code为文字,并因此解析为一个C常量。

Will NOT work. My guess is that MESH_K is pasted into the code as a literal, and therefore resolves to a C constant.

这篇关于25:警告:初始元素不是恒定的前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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