将int型转换为长指针

casting an int to a long pointer

本文关键字:指针 转换 int      更新时间:2023-10-16
int test = 1;
cout << (long *) &test <<endl; //=0x7fff7370cefc
cout << (long) test <<endl; //= 1

有人能解释一下第一种情况发生了什么吗?我得到了long> int,因此是一个乱码,但到底发生了什么?

在本例中:

cout << (long *) &test <<endl; //=0x7fff7370cefc

实际上是给出test变量地址的值,将其转换为一个长指针值然后打印。这正是你应该得到的。