gpt4 book ai didi

c - 为空结构启用警告

转载 作者:太空狗 更新时间:2023-10-29 14:59:15 25 4
gpt4 key购买 nike

我有一个关于 C 中的空结构的问题。据我阅读标准可以看出,它们似乎是不允许的:

6.2.5-20

— A structure type describes a sequentially allocated nonempty set of member objects (and, in certain circumstances, an incomplete array), each of which has an optionally specified name and possibly distinct type.

因此,在尝试编译类似以下内容时不足为奇:

struct foo
{
};

在 MS VS 中,会抛出一些错误:

error C2016: C requires that a struct or union has at least one member

但是,当使用 gcc -Wall -Werror 编译相同的代码时,没有看到任何错误。所以……

  1. 我是否正确阅读了 C 中不允许允许的规范? (更令人惊讶的是,微软做对了吗?!)
  2. 是否有一个选项可以传递给 gcc 以使其捕获此问题?

最佳答案

  1. 是的,没有成员的结构类型在 C 中是无效的。

  2. -Werror -pedantic with gcc 将停止翻译。

关于c - 为空结构启用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15415351/

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