#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include<string.h> #include<malloc.h> struct S { int n; int arr[];//大小未知 //这个数组就是柔性数组 }; int main() { //期望arr的大小是10个整形 struct S *ps = (struct S*)malloc(sizeof(struct S)+10*sizeof(int)); ps->n = 10; int i = 0; for (i = 0; i < 10; i++) { ps->arr[i] = i; } //增加 struct S* ptr = (struct S*)realloc(ps, sizeof(struct S) + 20 * sizeof(int)); if (ptr != NULL) { ps = ptr; } //使用 //释放 free(ps); ps = NULL; /*struct S s = { 0 }; printf("%d\n", sizeof(s));*/ return 0; }