I am attempting to map a simple struct to an underlying buffer as follows, where modifying the struct also modifies the buffer:
#[repr(C, packed)] pub struct User { id: u8, username: [u8; 20], } fn main() { let buf = [97u8; 21]; let mut u: User = unsafe { std::ptr::read(buf.as_ptr() as *const _) }; let buf_addr = &buf[0] as *const u8; let id_addr = &u.id as *const u8; println!("buf addr: {:p} id addr: {:p} id val: {}", buf_addr, id_addr, u.id); assert_eq!(buf_addr, id_addr); // TODO addresses not equal u.id = 10; println!("id val: {}", u.id); println!("{:?}", &buf); assert_eq!(buf[0], u.id); // TODO buffer not updated } However the starting address of the buffer is different to the address of the first member in the struct and modifying the struct does not modify the buffer. What is wrong with the above example?