柔性数组是C99的扩展,简而言之就是一个在struct结构里的标识占位符(不占结构struct的空间),只能在堆上生成
示例代码:
int main(int argc, char *aggv[])
{
struct flexible_array
{
int size;
char str[0];
};
struct flexible_array* fa = (struct flexible_array*)malloc(sizeof(struct flexible_array) + 100);
fa->size = 100;
memset(fa->str, 0, fa->size);
memcpy(fa->str, "This is a string", strlen("This is a string"));
printf("fa->size: %d\nfa->a: %s\n", fa->size, fa->str);
printf("size: %d\n", sizeof(fa));
getchar();
return 0;
}
运行结果如下:
fa->size: 100
fa->a: This is a string
size: 4