2016-04-25 61 views
0

下記のビットフィールドを宣言しました。C言語のビットフィールドをグローバルとして初期化できません

struct { 
    volatile uint8_t screenflag:1; 
    volatile uint8_t logoflag:1; 
    volatile uint8_t oledflag:1; 
    volatile uint8_t animationflag:1; 
    volatile uint8_t clockdialflag:1; 
    volatile uint8_t update_screen:1; 
    volatile uint8_t BLE_activity:1; 
    uint8_t ble_status:1; 
} oled_flag; 

しかし、私はmain()関数のうち、ビットフィールドの要素を初期化しようとしたときにコンパイルする際には、次のエラーを示しています。

....\Src\main.c(95): warning: #77-D: this declaration has no storage class or type specifier oled_flag.screenflag=1; ....\Src\main.c(95): error: #147: declaration is incompatible with "struct oled_flag" (declared at line 92)
oled_flag.screenflag=1; ....\Src\main.c(95): error: #65: expected a ";" oled_flag.screenflag=1; ....\Src\main.c(96): warning: #77-D: this declaration has no storage class or type specifier
oled_flag.logoflag=0; ....\Src\main.c(96): error: #147: declaration is incompatible with "struct oled_flag" (declared at line 95) oled_flag.logoflag=0; ....\Src\main.c(96): error: #65: expected a ";" oled_flag.logoflag=0; ....\Src\main.c(97): warning:

77-D: this declaration has no storage class or type specifier oled_flag.oledflag=1; ....\Src\main.c(97): error: #147: declaration

is incompatible with "struct oled_flag" (declared at line 96) oled_flag.oledflag=1; ....\Src\main.c(97): error: #65: expected a ";" oled_flag.oledflag=1; ....\Src\main.c(98): warning:

77-D: this declaration has no storage class or type specifier oled_flag.animationflag=0; ....\Src\main.c(98): error: #147:

declaration is incompatible with "struct oled_flag" (declared at line 97) oled_flag.animationflag=0; ....\Src\main.c(98): error: #65: expected a ";"
oled_flag.animationflag=0; ....\Src\main.c(99): warning: #77-D: this declaration has no storage class or type specifier
oled_flag.clockdialflag=1; ....\Src\main.c(99): error: #147: declaration is incompatible with "struct oled_flag" (declared at line 98) oled_flag.clockdialflag=1; ....\Src\main.c(99): error: #65: expected a ";"
oled_flag.clockdialflag=1; ....\Src\main.c(100): warning: #77-D: this declaration has no storage class or type specifier

など。

初期化コードは次のとおりです。

oled_flag.screenflag=1; 
oled_flag.logoflag=0; 
oled_flag.oledflag=1; 
oled_flag.animationflag=0; 
oled_flag.clockdialflag=1; 
oled_flag.update_screen=0; 
oled_flag.BLE_activity=0; 
oled_flag.ble_status=1; 

しかし、私はmain()関数内のビットフィールドの要素を初期化するとき、それは正常に動作します。

+0

'ビットを表すものではありませんメンバーをフィールドは任意のデータ型にすることができ、volatileまたはconst修飾子 ' – sjsam

+0

を持つことができます。したがって、実際には 'oled_flag'とは何ですか? –

+0

コメントをフォローアップする#1あなたのコンパイラがこれについて不平を言っていないのはなぜですか?それともそれを無視するほど賢いですか? – sjsam

答えて

2

あなたが関数内のステートメントを移動したり、その宣言と一緒にグローバル変数を初期化する必要がC.で関数外文持つことはできません。

struct { 
    volatile uint8_t screenflag:1; 
    volatile uint8_t logoflag:1; 
    volatile uint8_t oledflag:1; 
    volatile uint8_t animationflag:1; 
    volatile uint8_t clockdialflag:1; 
    volatile uint8_t update_screen:1; 
    volatile uint8_t BLE_activity:1; 
    uint8_t ble_status:1; 
} oled_flag = { 
    .screenflag = 1, 
    .logoflag = 0, 
    .oledflag = 1, 
    .animationflag = 0, 
    .clockdialflag = 1, 
    .update_screen = 0, 
    .BLE_activity = 0, 
    .ble_status = 1 
}; 
+0

これは、最新のC標準の初期化メソッドであると理解していますか?古いバージョンの初期化は '{1,0,1,0,1,0,0,1}'でしょうか? (ちょっと好奇心が強いです。) –

+0

@PaulOgilvie私が使った指定された初期化子は、C99から入手可能です(最新のC標準はC11です)。 C99より前のバージョンでは、コメントの構文を使用する必要があります。 – Kninnug

+0

ありがとう、コンパイルされました。それは私にとって新しい情報です。私は、inbuiltのデータ型を持つ変数がどのように初期化されるかのように、bitfeildの要素を初期化できると考えました。 –

関連する問題