我有这个结构:
typedef struct
{
union{
int bytex[8];
int bytey[7];
}Value ;
int cod1;
int cod;
} test;
并且想要初始化常量
test
如下:const test T{
.Value.bytex = {0x11,0x22,0x33,0x44,0x11,0x22,0x33,0x44},
.cod1=0,
.cod=1,
};
我收到以下错误
Expected primary-expression before '.' token
然而这个初始化是正确的:
const test T{
{0x11,0x22,0x33,0x44,0x11,0x22,0x33,0x44},
.cod1=0,
.cod=1,
};
你有什么主意吗 ?
最佳答案
首先,这与结构/union 初始化语法不接近。使固定:
const test T =
{
.Value.bytex = { 0x11,0x22,0x33,0x44,0x11,0x22,0x33,0x44 },
.cod1 = 0,
.cod = 1,
};
其次,如果您可以选择使用标准 C,则可以删除内部变量名称:
typedef struct
{
union {
int bytex[8];
int bytey[7];
};
int cod1;
int cod;
} test;
const test T =
{
.bytex = { 0x11,0x22,0x33,0x44,0x11,0x22,0x33,0x44 },
.cod1 = 0,
.cod = 1,
};
关于c - 初始化结构内的数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52971866/