簡體   English   中英

使用typedef結構時出錯

[英]error when using typedef struct

我有以下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編譯器。 任何幫助揭開這種謝意的幫助。

BPM1的值可能會更改,因此編譯器將不允許將其作為初始化列表的參數。

如果要使用命名常量,請嘗試使用enum

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM