C中编译时的数组定义 [英] Array definition at compile time in C

查看:83
本文介绍了C中编译时的数组定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

有人可以告诉我如何使用预处理器在编译时初始化数组。

以上是我认为的替代方案。我的实际问题如下:



  //  我有以下两种结构 
typedef struct
{
float rahul [ 6 ];
} RahulT;

typedef struct Max {
int ax;
const float * f1;
const float * f2;
} tMax;

// 对结构进行初始化并声明指向它的指针。
const RahulT RahulTvar = {{ 3 4 6 8 9 7 }};
const RahulT * pRahulTvar =& RahulTvar;

// 在第二个静态const str中使用结构的数组。
static const tMax teststr = {
11 / * Nx:* /
&(pRahulTvar-> ; rahul [ 0 ]), // 这是给予错误:错误C2099:初始化程序不是常数
&(RahulTvar.rahul [ 0 ]) // 工作正常

};





为什么会出现问题。什么是可能的解决方案。

我想使用指针选项来保存临时内存复制操作。但是在指针的情况下它会给出问题。

解决方案

错误消息有点明确:初始化程序不是常量 - 它不是。



就编译器而言,对象的地址不是常量值,因为它是由链接器定义的,它将程序及其数据定位在内存空间中。编译器没有 - 如果根据它的大小定义所有内容,并让链接器整理出实际地址。



但你不能使用的值在任何情况下任何变量都是常数值 - 这就是你要做的事情。

即使你这样做了:

 < span class =code-keyword> int  ii =  6 ; 
静态 const tMax teststr = {
ii,
0
0 };

你会得到同样的错误!


我不太明白为什么我的建议在C ++中有效,但在C中却没有。所以我检查了其他人是否遇到了同样的问题。事实上,我在SO上找到了一个条目,第二个响应提供了一个可理解的解释,以及可能对您的情况有所帮助的解决方法:



http://stackoverflow.com/questions/12217165/const-pointer-pointing-to-a-const -pointer [ ^ ]

Hi All
can anyone please tell me how to initialize an array at compile time using preprocessor.
Above is the aternative That I thought. My actual problem is as below:

//I have following two structure
typedef struct
{
 float rahul[6];
}RahulT;

typedef struct Max{					
   int ax;
   const float* f1;
   const float* f2;
} tMax;

//intitialze the structure and declare a pointer to it.
const RahulT  RahulTvar={{3,4,6,8,9,7}};
const RahulT*  pRahulTvar = &RahulTvar;

//using the structures's array in second static const str.
static const tMax teststr = {
   11 /* Nx:  */, 
  &(pRahulTvar->rahul[0]),//this is giving error:  error C2099: initializer is not a constant
  &(RahulTvar.rahul[0])// it is working properly
  
};



Why is the problem is there. and what is the possible solution.
I want to use pointer option to save temporary memory copy operation. But in case of pointer it is giving problem.

解决方案

The error message is kinda explicit: "initializer is not a constant" - which it isn't.

The address of an object is not a constant value as far as the compiler is concerned, because it is defined by the linker which locates the program and its data inside the memory space. The compiler doesn't - if defines everything in terms of it's size, and lets the linker sort out the actual address.

But you can't use the value of any variable as a constant value under any circumstances - which is what you are trying to do.
Even if you did this:

int ii = 6;
static const tMax teststr = {
    ii,
    0,
    0};

You would get the same error!


I couldn't quite understand why the suggestion I made works in C++, but not in C. So I checked to see if others came across the same problem. Indeed I found an entry on SO, and the second response offers a halfway understandable explanation as well as a workaround that might help in your situation:

http://stackoverflow.com/questions/12217165/const-pointer-pointing-to-a-const-pointer[^]


这篇关于C中编译时的数组定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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