且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

省略指向结构的指针的前向声明是否有效?

更新时间:2023-11-28 11:07:46

如果表示形式相同,则表示大小相同.

If the representations are the same, that implies that the sizes are the same.

C标准的第6.2.6.1p2节状态:

存储在任何其他对象类型的非位字段对象中的值[ed.不是未签名的字符]由 n×CHAR_BIT 位组成,其中 n 是对象的大小该类型(以字节为单位).该值可以复制到一个类型的对象中 unsigned char [n] (例如,通过 memcpy );结果字节集为称为值的对象表示形式.位字段由 m 位组成,其中 m 是指定的大小对于位域.对象表示形式是该位字段包含 m 位,该位包含该位字段.相同的Tw o值(NaN除外)对象表示比较相等,但比较的值相等可能具有不同的对象表示形式.

Values stored in non-bit-field objects of any other object type [ed. not unsigned char] consist of n×CHAR_BIT bits, where n is the size of an object of that type, in bytes. The value may be copied into an object of type unsigned char [n] (e.g., by memcpy); the resulting set of bytes is called the object representation of the value. Values stored in bit-fields consist of m bits, where m is the size specified for the bit-field. The object representation is the set of m bits the bit-field comprises in the addressable storage unit holding it. Tw o values (other than NaNs) with the same object representation compare equal, but values that compare equal may have different object representations.

作为示例,gcc下的 int short 都使用二进制补码,但是它们没有相同的对象表示形式,因为一个是4个字节,另一个是是2个字节.

As an example, an int and a short under gcc both use two's complement but they don't have the same object representation because one is 4 bytes and the other is 2 bytes.