我知道它是一个整数类型,可以在不丢失数据的情况下强制转换为指针,但为什么我要这样做呢?使用整数类型有什么优势void*可以保存指针和THE_REAL_TYPE*指针算术?
编辑
标记为"已被询问"的问题没有回答这个问题.问题是如果使用intptr_t作为一般的替换void*是一个好主意,那里的答案似乎是"不要使用intptr_t",所以我的问题仍然有效:什么是一个很好的用例intptr_t?
我试图将地址的值存储在非指针int变量中,当我尝试转换它时,我得到编译错误"无效转换从'int*'到'int'"这是我正在使用的代码:
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
vector<int> test;
int main() {
int *ip;
int pointervalue = 50;
int thatvalue = 1;
ip = &pointervalue;
thatvalue = ip;
cout << ip << endl;
test.push_back(thatvalue);
cout << test[0] << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)