We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 12f95ff commit 058141fCopy full SHA for 058141f
src/c-tips/index.md
@@ -357,8 +357,13 @@ struct Foo {
357
358
fn main() {
359
let v = Foo { x: 0, y: 0, z: 0 };
360
- // Unsafe is required to borrow a field of a packed struct.
361
- unsafe { println!("{:p} {:p} {:p}", &v.x, &v.y, &v.z) };
+ // References must always be aligned, so to check the addresses of the
+ // struct's fields, we use `std::ptr::addr_of!()` to get a raw pointer
362
+ // instead of just printing `&v.x`.
363
+ let px = std::ptr::addr_of!(v.x);
364
+ let py = std::ptr::addr_of!(v.y);
365
+ let pz = std::ptr::addr_of!(v.z);
366
+ println!("{:p} {:p} {:p}", px, py, pz);
367
}
368
369
// 0x7ffd33598490 0x7ffd33598492 0x7ffd33598493
0 commit comments