c – 给定结构的大小

前端之家收集整理的这篇文章主要介绍了c – 给定结构的大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
struct x
{
    char a : 1; // statement 1
    char c : 3; // statement 2
};

如果这个结构将是什么大小.声明1和2的含义是什么?

解决方法

这些语句声明了Bit字段.
它意味着占用1位的存储器而c占用3位的存储器.

结构的大小将是:
至少4位
填充(比特)

最有可能的是,它将是8位,即:1个字节

因为,
如果一系列位字段没有达到int的大小,则可以进行填充.填充量由结构构件的对准特征确定.

什么是比特字段?
@L_301_0@

Both C and C++ allow integer members to be stored into memory spaces smaller than the compiler would ordinarily allow. These space-saving structure members are called bit fields,and their width in bits can be explicitly declared. Bit fields are used in programs that must force a data structure to correspond to a fixed hardware representation and are unlikely to be portable.

The Syntax for declaring a bit field is as follows:

>>-type_specifier–+————+–:–constant_expression–;—><
‘-declarator-‘

A bit field declaration contains a type specifier followed by an optional declarator,a colon,a constant integer expression that indicates the field width in bits,and a semicolon. A bit field declaration may not use either of the type qualifiers,const or volatile.

猜你在找的C&C++相关文章