我有一个看起来像这样的结构:
typedef struct
{
uint32_t a;
uint32_t b;
uint32_t c[5];
uint32_t d;
} MY_STRUCT_T;
我要初始化
c
,按名称,为非零值。我希望其他一切都为 0。如
c
不是数组,我可以这样做:static MY_STRUCT_T my_struct = {.b = 1};
我知道我可以做到这一点:
static MY_STRUCT_T my_struct = {.c[0]=5,
.c[1]=5,
.c[2]=5,
.c[3]=5,
.c[4]=5};
但我想知道是否有我不知道的更优雅的语法:
就像是:
static MY_STRUCT_T my_struct = {.c[] = {5,5,5,5,5}};
我已阅读以下内容,但他们没有回答这个问题:
Initializing a struct to 0
Initialize/reset struct to zero/null
A better way to initialize a static array member of a class in C++ ( const would be preferred though )
How to initialize all members of an array to the same value?
最佳答案
所以我写了这个问题,然后试验了一段时间,发现以下方法可行:
static MY_STRUCT_T my_struct = {.c={5,5,5,5,5}};
关于c - 按名称初始化结构的数组成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39458531/