C/C++ 中定义的具有灵活数组成员的结构体的用法在哪里?标准?

发布于 2025-01-13 05:22:38 字数 396 浏览 1 评论 0原文

如果我有这样的代码

struct s { int x; char b[]; };

int main() {
    struct s s[10];
}

并使用“gcc -O2 -W -Wall -pedantic”进行编译,那么我得到:

<source>:4:14: warning: invalid use of structure with flexible array member [-Wpedantic]
    4 |     struct s s[10];
      |              ^

gcc 是完全正确的。具有灵活数组成员的结构不能这样工作。

C/C++ 标准中的什么地方定义了这个?

If I have code like this

struct s { int x; char b[]; };

int main() {
    struct s s[10];
}

and I compile with "gcc -O2 -W -Wall -pedantic" then I get:

<source>:4:14: warning: invalid use of structure with flexible array member [-Wpedantic]
    4 |     struct s s[10];
      |              ^

And gcc is totally right. Structs with flexible array members can't work like that.

Where in the C/C++ standards is this defined?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2025-01-20 05:22:38

在 C 语言中,它是 §6.7.2.1,第 3 段:

结构或联合不得包含不完整或函数类型的成员,...除非具有多个命名成员的结构的最后一个成员可能具有不完整的数组类型;这样的结构(以及可能递归地包含属于此类结构的成员的任何联合)不应是结构的成员或数组的元素。

正如 @phuclv注释,C++没有这个功能。

In C, it's §6.7.2.1, paragraph 3:

A structure or union shall not contain a member with incomplete or function type,… except that the last member of a structure with more than one named member may have incomplete array type; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array.

As @phuclv points out in a comment, C++ doesn't have this feature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文