RUST 学习笔记2:
提示:常用指针转换
use std::os::raw::c_void;
fn main() {
struct User{age:i32};
let mut user = User{age:11};
let p = &user as *const User as i32;
println!("raddr:{:0x}",p);
//获取user raw pointer
let rp_user = &mut user as *mut User;
//获取cpointer
let cp_user = rp_user as *mut c_void;
//windapi longptr
let lptr_user = cp_user as isize;
println!("caddr:{:0x}",&lptr_user);
//解引用
//raw pointer 解引用
let user1 = unsafe{&*rp_user};
println!("age:{}",user1.age);
//转换成罗指针后解引用
//cpointer 解引用
let user2 =unsafe{&*(cp_user as *const User)};
println!("age:{}",user2.age);
//longptr 解引用
let user3 =unsafe{&*(lptr_user as *const User)};
println!("age:{}",user3.age);
//可变解引用
let mut user4 = unsafe{&mut *(cp_user as *mut User)};
user4.age = 16;
println!("age:{}",user4.age);
}