用C空的结构 [英] Empty structure in C

查看:119
本文介绍了用C空的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有没有成员(目前)的结构,我想知道是否有可能燮PSS警告$ P $ I得到:

I have a structure with no members (for the moment) and I would like to know if it is possible to suppress the warning I get:

warning: struct has no members

是否有可能增加一个成员,并保持的sizeof 的结构零?任何其他解决办法?

Is it possible to add a member and keep the sizeof the struct zero? Any other solution?

推荐答案

在C空结构的行为是依赖编译器对C ++它是规范的一部分(<一个href=\"http://stackoverflow.com/questions/621616/c-what-is-the-size-of-an-object-of-an-empty-class\">explanations这里)

In c the behaviour of an empty structure is compiler dependent versus c++ where it is part of the spec (explanations here)

C ++

  与成员和基类对象的空序列类是一个空类。一个空的类类型的完整的对象和成员子对象应具有非零的大小。

C++
A class with an empty sequence of members and base class objects is an empty class. Complete objects and member subobjects of an empty class type shall have nonzero size.

在C中,它是较为明朗,因为C99标准有这意味着真正空的结构不允许一些语言(见TrayMan的答案),但是很多编译器都允许它(如<一个href=\"http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Empty-Structures.html#Empty%20Structures\">gcc).

in C it is rather more murky since the c99 standard has some language which implies that truly empty structures aren't allowed (see TrayMan's answer) but many compilers do allow it (e.g gcc).

由于这是编译器相关的这是不可能的,你会得到真正的便携式code在这种情况下。由于这种非便携的方式来共进晚餐preSS警告可能是你最好的选择。

Since this is compiler dependent it is unlikely that you will get truly portable code in this case. As such non portable ways to suppress the warning may be your best bet.

  • In VS you would use #pragma warning
  • in GCC from 4.2.1 you have Diagnostic Pragmas

这篇关于用C空的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆