类型声明
具有至少一个构件的结构可以在结构的末端另外包含单个未指定长度的阵列构件。这称为灵活数组成员:struct ex1
{
size_t foo;
int flex[];
};
struct ex2_header
{
int foo;
char bar;
};
struct ex2
{
struct ex2_header hdr;
int flex[];
};
/* Merged ex2_header and ex2 structures. */
struct ex3
{
int foo;
char bar;
int flex[];
};
对大小和填充的影响
在计算结构的大小时,可伸缩数组成员被视为没有大小,尽管该成员与该结构的前一个成员之间仍然存在填充:/* Prints "8,8" on my machine, so there is no padding. */
printf("%zu,%zu\n", sizeof(size_t), sizeof(struct ex1));
/* Also prints "8,8" on my machine, so there is no padding in the ex2 structure itself. */
printf("%zu,%zu\n", sizeof(struct ex2_header), sizeof(struct ex2));
/* Prints