C语言 不定长数组
#include <stdio.h>
#include <malloc.h>
struct student {
int age;
};
struct data {
int len;
// 不占用空间
struct student students[0];
};
int main() {
struct data* d = malloc(sizeof(struct data) + 2 * sizeof(struct student));
d->len = 2;
d->students[0].age = 10;
d->students[1].age = 11;
// 结果是4
printf("%d\n", sizeof(struct data));
// 内存对齐,结果是8
printf("%d\n", sizeof(d));
// 结果是0
printf("%d\n", sizeof(d->students));
free(d);
return 0;
}