能否位字段只能是结构/联合的领域,从来不与QUOT;正常","独立"变量? [英] Can bit-fields only be fields of a structure/union, never "normal", "stand-alone" variables?

查看:92
本文介绍了能否位字段只能是结构/联合的领域,从来不与QUOT;正常","独立"变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

位字段的字段部分似乎表明,他们只能是结构或联合中的字段。

The field part of bit-fields seems to suggest that they can only be fields inside a structure or union.

位域可以是一个典型的独立的变量,任何集合数据类型一样工会或结构之外如下:

Can a bit-field be a typical "stand-alone" variable, outside any aggregate data-type like union or structure as follows:

   int sum:6;   //Can this work as a declaration statement?
   sum=4;

如果没有,为什么?如果位字段基本上都是打算用更少的内存,我们为什么不能声明任何变量作为位字段,如果我们知道它不会超过大小?

If not, why so? If bit-fields are basically intended to use less memory, why can't we declare any variable as a bit-field if we know it won't exceed that size?

推荐答案

位字段只有结构或联合的一部分,因为那是什么的C的标准允许。这本来是可以决定不同。为什么委员会决定写标准的方式是,你得去问他们。

Bit-fields are only part of structs or unions because that's what the C standard allows. It would have been possible to decide differently. Why the committee decided to write the standard the way it is, you would have to ask them.

要发现在C99标准的信息:

To find the information in the C99 standard:

从表的内容:

6.7声明

6.7.2类型说明符(好吧,这个人是有点晦涩)

6.7.2 Type specifiers (okay, this one is a little bit obscure)

6.7.2.1结构和联合说明符。

6.7.2.1 Structure and union specifiers.

一个位字段是 declarator_opt:恒恩pression 语法的一部分。此语法只允许在这里,和一个后果是,一个人不能在别处声明位字段。

One bit-field is the declarator_opt : constant-expression part of the syntax. This syntax is only allowed here, and a consequence is that one cannot declare bit-fields elsewhere.

语法告诉你有一个位字段的名称可以被省略,如果你好奇的这类信息。第6.7.2.1:3更进一步,禁止名字0长度位字段。

The syntax tells you that the name of a bit-field can optionally be omitted, if you were curious for this sort of information. Clause 6.7.2.1:3 goes further and prohibits names for 0-length bit-fields.

这篇关于能否位字段只能是结构/联合的领域,从来不与QUOT;正常","独立"变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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