C程序设计常量数组初始化 [英] C Programming const array initializer

查看:133
本文介绍了C程序设计常量数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC告诉我以下内容: Transformations.h:16:1:错误:初始元素不是常数

下面是code:

  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 3const 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}; // FAIL无效美孚(无效)
{
    const int的xyzOrigin [NUM_DIMENSIONS] = {X_ORIGIN,Y_ORIGIN,Z_ORIGIN}; // 好
}

另外,您可以编译code和C ++而不是C

GCC tells me the following: Transformations.h:16:1: error: initializer element is not constant

Here is the code:

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};

解决方案

You can't do this at global scope in C, only at local scope, i.e. within a function:

#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, Y_ORIGIN, Z_ORIGIN}; // FAIL

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

Alternatively you could compile the code as C++ rather than C.

这篇关于C程序设计常量数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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