gpt4 book ai didi

c - 为什么灵活数组成员必须位于结构的末尾,但具有灵活数组的结构则不然?

转载 作者:行者123 更新时间:2023-11-30 20:22:42 25 4
gpt4 key购买 nike

一些具有灵活数组的结构:

struct SomeArray { unsigned length; int array[]; };

此代码 gcc(版本 4.9.2)编译没有错误:

struct s1{ unsigned length; SomeArray some_array; const char * string; } ss1;

这是如何工作的?

最佳答案

来自标准:

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply.

这似乎是灵活数组成员被忽略的情况之一。因此,ss1.some_array 的大小不包含 ss1.some_array.array 的空间。

关于c - 为什么灵活数组成员必须位于结构的末尾,但具有灵活数组的结构则不然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38958934/

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