我遇到过这段代码:
struct test
{
uint32 num_fields;
char array_field [];
};
Run Code Online (Sandbox Code Playgroud)
我怎么能理解array_field?这是C语言的gcc扩展吗?
P.P*_*.P. 15
它是一个C99功能,称为灵活数组成员,通常用于创建可变长度数组.
它只能被指定为的最后一个成员结构,而无需指定尺寸(如在array_field [];).
例如,您可以执行以下操作,该成员arr将为其分配5个字节:
struct flexi_example
{
int data;
char arr[];
};
struct flexi_example *obj;
obj = malloc(sizeof (struct flexi_example) + 5);
Run Code Online (Sandbox Code Playgroud)
它的优点/缺点在这里讨论: