加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C编程const数组初始化器

发布时间:2020-12-16 05:37:10 所属栏目:百科 来源:网络整理
导读:GCC告诉我如下:Transformations.h:16:1:error:initializer元素不是常量 这是代码: const int X_ORIGIN = 1233086; const int Y_ORIGIN = -4728071; const int Z_ORIGIN = 4085704;const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN,Y_ORIGIN,Z_ORIGIN}
GCC告诉我如下:Transformations.h:16:1:error:initializer元素不是常量

这是代码:

const int X_ORIGIN = 1233086;             
const int Y_ORIGIN = -4728071;              
const int Z_ORIGIN = 4085704;
const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN,Y_ORIGIN,Z_ORIGIN};

解决方法

您不能在C的全局范围内执行此操作,仅在本地范围内,即在函数中:
#define NUM_DIMENSIONS 3

const int X_ORIGIN = 1233086;             
const int Y_ORIGIN = -4728071;              
const int Z_ORIGIN = 4085704;

const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN,Z_ORIGIN}; // FAIL

void foo(void)
{
    const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN,Z_ORIGIN}; // OK
}

或者,您可以将代码编译为C而不是C.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读