如何在预处理器时间内计算数组大小?
How to calculate array size in preprocessor time?
我想创建一个数组,其大小是在预处理器中通过定义计算出来的。当我尝试初始化此数组 "memory" 时,出现以下错误:
错误:表达式必须是整数常量表达式
看起来给出大小的表达式似乎是可变的,这很奇怪,因为它是根据定义计算的。请参阅以下代码:
#define SAMPLE_FREQUENCY_HZ 48000
#define SAMPLE_INTERVAL_MS ((1.0 / SAMPLE_FREQUENCY_HZ) * 1000.0)
#define MAX_DELAYLINE_LENGHT_MS 250
#define MAX_DELAYLINE_LENGHT_ELEM (MAX_DELAYLINE_LENGHT_MS / SAMPLE_INTERVAL_MS)
typedef struct {
float delay;
int size;
// float *memory;
float memory[MAX_DELAYLINE_LENGHT_ELEM];
float *start;
float *end;
float *writePointer;
float *readPointer;
} delayline_t;
有人可以解释一下这个问题并给我一个解决方案,以需要的方式解决这个问题。谢谢!
MAX_DELAYLINE_LENGHT_ELEM
是一个 float/double 但数组大小必须是整数。
解析为:
(250 / (1.0 / 48000*1000.0)) =12222.0
但对于编译器来说它仍然是一个双精度值。
确定结构中成员数组大小的表达式,必须是整数常量表达式。
6.6 Constant expressions
- An integer constant expression 117) shall have integer type and shall only have operands
that are integer constants, enumeration constants, character constants, sizeof
expressions whose results are integer constants, _Alignof expressions, and floating
constants that are the immediate operands of casts. Cast operators in an integer constant
expression shall only convert arithmetic types to integer types, except as part of an
operand to the sizeof or _Alignof operator.
如您所见,此表达式中不允许使用浮点类型。
您必须手动预先计算宏的值,将它们四舍五入为整数值,然后将它们包含到程序中。
数组大小必须是整数常量。
建议 3 处更改:A) 更改为纳秒。 B) 使用 rounded 整数计算 2) "LENGTH"(拼写)
#define SAMPLE_FREQUENCY_HZ (48000 /* Hertz */ )
#define SAMPLE_INTERVAL_ns \
((1000UL*1000*1000 + SAMPLE_FREQUENCY_HZ/2)/ SAMPLE_FREQUENCY_HZ)
#define MAX_DELAYLINE_LENGTH_ns (250UL*1000*1000 /* nano seconds */)
#define MAX_DELAYLINE_LENGTH_ELEM \
((MAX_DELAYLINE_LENGTH_ns + SAMPLE_INTERVAL_ns/2)/ SAMPLE_INTERVAL_ns)
我想创建一个数组,其大小是在预处理器中通过定义计算出来的。当我尝试初始化此数组 "memory" 时,出现以下错误:
错误:表达式必须是整数常量表达式
看起来给出大小的表达式似乎是可变的,这很奇怪,因为它是根据定义计算的。请参阅以下代码:
#define SAMPLE_FREQUENCY_HZ 48000
#define SAMPLE_INTERVAL_MS ((1.0 / SAMPLE_FREQUENCY_HZ) * 1000.0)
#define MAX_DELAYLINE_LENGHT_MS 250
#define MAX_DELAYLINE_LENGHT_ELEM (MAX_DELAYLINE_LENGHT_MS / SAMPLE_INTERVAL_MS)
typedef struct {
float delay;
int size;
// float *memory;
float memory[MAX_DELAYLINE_LENGHT_ELEM];
float *start;
float *end;
float *writePointer;
float *readPointer;
} delayline_t;
有人可以解释一下这个问题并给我一个解决方案,以需要的方式解决这个问题。谢谢!
MAX_DELAYLINE_LENGHT_ELEM
是一个 float/double 但数组大小必须是整数。
解析为:
(250 / (1.0 / 48000*1000.0)) =12222.0
但对于编译器来说它仍然是一个双精度值。
确定结构中成员数组大小的表达式,必须是整数常量表达式。
6.6 Constant expressions
- An integer constant expression 117) shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, _Alignof expressions, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof or _Alignof operator.
如您所见,此表达式中不允许使用浮点类型。
您必须手动预先计算宏的值,将它们四舍五入为整数值,然后将它们包含到程序中。
数组大小必须是整数常量。
建议 3 处更改:A) 更改为纳秒。 B) 使用 rounded 整数计算 2) "LENGTH"(拼写)
#define SAMPLE_FREQUENCY_HZ (48000 /* Hertz */ )
#define SAMPLE_INTERVAL_ns \
((1000UL*1000*1000 + SAMPLE_FREQUENCY_HZ/2)/ SAMPLE_FREQUENCY_HZ)
#define MAX_DELAYLINE_LENGTH_ns (250UL*1000*1000 /* nano seconds */)
#define MAX_DELAYLINE_LENGTH_ELEM \
((MAX_DELAYLINE_LENGTH_ns + SAMPLE_INTERVAL_ns/2)/ SAMPLE_INTERVAL_ns)