gpt4 book ai didi

c - 结构中的零长度数组有何用处?

转载 作者:行者123 更新时间:2023-12-04 09:13:49 24 4
gpt4 key购买 nike

<分区>

引自this site ,

Zero-length arrays are allowed in GNU C. They are very useful as the last element of a structure that is really a header for a variable-length object.

代码:

 struct line {
int length;
char contents[0];
};

struct line *thisline = (struct line *)malloc(sizeof (struct line) + this_length);
thisline->length = this_length;

我真的不明白他们的意思:“结构的最后一个元素实际上是可变长度对象的 header ”。有人可以详细说明这句话吗?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com