c 结构体最后一个成员使用长度为0数组
变长数组,不用指针,可以只分配一次内存,释放一次内存,方便内存管理
demo.c
#include <stdio.h> #include <stdlib.h> #include <string.h> struct demo { char ch1; char ch2; char str[0]; }; struct demo1 { char ch1; char ch2; }; int main() { printf("sizeof demo:%d\n", sizeof(struct demo)); printf("sizeof demo1:%d\n", sizeof(struct demo1)); struct demo* buff = malloc(sizeof(struct demo) + 10); memset(buff->str, '\0', 10); memcpy(buff->str, "hello", 5); printf("buff->str:%s\n", buff->str); return 0; }
Please call me JiangYouDang!