使用typedef结构时出错

时间:2013-11-29 04:58:40

标签: c struct typedef

我有以下typedef结构

typedef unsigned int NOTE_FREQ;
/*******A_MUSIC_ELEMENT structure****************/ 
typedef struct { 
    NOTE_FREQ frequencyValue; 
    int duration; 
} A_MUSIC_ELEMENT; 

现在我想创建一个A_MUSIC_ELEMENT数组并使用特定值。

A_MUSIC_ELEMENT ZTitleScreen[] = {{60, 20},{80, 50}}; 

它编译得很好。但为了使事情更具可读性,我尝试设置

int BPM1 = 60;
int BPM2 = 80;
int TIME1 = 20;
int TIME2 = 50;
A_MUSIC_ELEMENT ZTitleScreen[] = {{BPM1, TIME1},{BPM2, TIME2}}; 

我得到一个错误说:

constant expression required 

我不知道为什么因为它应该是同一件事。我正在使用Windows 8,mplab x IDE,高科技c编译器。任何帮助神秘化的感谢。

2 个答案:

答案 0 :(得分:4)

BPM1的值可能会发生变化,因此编译器不会将其作为初始化列表的参数。

如果要使用命名常量,请尝试使用enum

答案 1 :(得分:1)

6.7.9初始化

     4. All the expressions in an initializer for an object that has static or 
thread storage duration shall be constant expressions or string literals.

以下是初始化结构的不同方法......

struct date date1= { 9, 5, 1982};           // Initializing structure
struct date date2 = { .day = 9, .month = 5};// Designated initializer
struct date date3 = {0};                    // Initializing all members to 0
struct date date4;
struct date date5 = date1;                  // Initialization using variable

date4.day = 9;      // Member wise initialization
date4.month = 5;    // Member wise initialization
date4.year = 1982;  // Member wise initialization