用位域初始化常量数组

时间:2019-08-20 12:15:32

标签: c arrays struct bit-fields

我想初始化一个const结构数组。这些结构具有位域成员。

以下是我的代码段:

typedef struct {
    unsigned int a : 1;
    unsigned int b : 1;
    unsigned int c : 1;
} Character;

const static Character Char[] =
{
    {.a = 0, .b = 0, .c = 1},
    {.a = 0, .b = 1, .c = 0},
    {.a = 1, .b = 0, .c = 1}
};

以这种方式尝试时,我遇到许多错误,例如unexpected initialization syntaxmissing ;

正确的方法是什么?

更新

我正在使用COSMIC编译器(CXSTM8)。我检查了它的用户指南,但在这方面找不到任何信息。

1 个答案:

答案 0 :(得分:3)

您提供的语法正确。 指定的初始化程序列表是在C99中引入的。

如果编译器不支持此功能,则需要选择下一个最佳选择。即初始化位域中的所有成员。

typedef struct {
    unsigned int a : 1;
    unsigned int b : 1;
    unsigned int c : 1;
} Character;

const static Character Char[] =
{
    {0, 0, 1},
    {0, 1, 0},
    {1, 0, 1}
};