且构网

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

结构体和结构体的第一个成员之间的指针别名

更新时间:2021-07-12 04:40:06

这段代码是正确的.尽管措辞各不相同,但所有版本的标准 C 和 C++ 都允许这样做.

This code is correct. All versions of Standard C and C++ allow this , although the wording varies.

没有严格的别名问题,因为您通过 int 类型的左值访问 int 类型的对象.当执行访问的左值与存储在内存位置的对象具有不同类型时,可能会应用严格的别名规则.

There's no strict aliasing issue because you access an object of type int via an lvalue of type int. The strict aliasing rule may apply when the lvalue doing the access has a different type to the object stored at the memory location .

您引用的文本涵盖了指针转换实际上指向了 int 对象.

The text you quoted covers that the pointer cast actually points to the int object.