柔性数组
1 | typedef struct { |
如何构建这个结构体呢?1
2
3
4
5
6
7
8
9
10
11Person *person(char const *name, int age, char const *intro) {
size_t intro_len = (intro ? strlen(intro) : 0) + 1; // char数组以'\0'结尾
Person *p = (Person *) malloc(sizeof(Person) + intro_len);
p->age = age;
p->name = name;
if (intro) {
strcpy(p->intro, intro);
} else {
p->intro[0]=0;
}
}