我正在编写一个 mex 文件(使用 C++),它将接受内存地址作为输入,并对位于该内存地址的数据进行操作。因为我被迫使用 MATLAB 作为我的环境,所以我的程序只能接受 MATLAB 数据类型作为输入(char、bool、float、double 和 int)。我怎样才能将我的输入值分配给一个指针?
伪代码:
// Outside of program
// double input_arg = hex2dec('00C2E4E8')
double *pointer;
pointer = (double *)input_arg;
// pointer == hex2dec('00C2E4E8')
基本上,这可以看作是我对类似于以下内容的指针的值进行硬编码:
double *pointer = (double *)hex2dec('00C2E4E8');
我收到错误:
error C2440: '=' : cannot convert from 'double' to 'double *'
我也尝试过使用 static/const/reinterpret/dynamic_cast,但我并不真正理解它们是如何工作的(而且我无法让它们工作)。是否有可能手动将内存地址值分配给指针?
最佳答案
您似乎不能使用浮点类型的 double
作为内存中地址的表示,它本质上是一个整数值。
我猜你的 input_arg
应该定义为 MATLAB int
类型,而不是 double
。
关于c++ - 显式设置指针的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6986151/