我有这样的结构:
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/