【发布时间】:2014-01-07 12:11:42
【问题描述】:
我正在尝试在 C 中的全局结构内定义一组常量变量。到目前为止,我有一个头文件“params.h”:
#ifndef PARAMS_H_INCLUDED
#define PARAMS_H_INCLUDED
typedef struct Params_s {
const int nSamples;
//Some other constants here...
} Params;
extern const Params params;
#endif
我还有一个文件“params.c”来定义我的常量:
#include "params.h"
Params params = {
8*1024, // nSamples
//Some other constants here...
}
当我尝试使用这些常量来定义诸如数组大小之类的东西时,就会出现我的问题。如果,在第三个 .c 文件中,我写:
#include "params.h"
//...Code here...
double p[params.nSamples];
然后我在数组大小上出现以下错误:
IntelliSense: expression must have a constant value
我是否错误地声明了我的常量?
【问题讨论】:
-
只使用预处理器定义(宏)代替:#define N_SAMPLES (8*1024)
-
是否再次初始化第三个.c文件中的Params结构?