[英]How to create a pointer to a buffer from char []
如果您有缓冲区的内存地址,并且该地址存储在char中,例如: char bufferAddress[] = "0024ABC3"
,如何使用bufferAddress
创建指针,以便可以访问缓冲区中的变量?
If you can get the string into a number, then you can try something like this: 如果可以将字符串转换成数字,则可以尝试如下操作:
void *ptr = reinterpret_cast<void*> (0x0024ABC3);
There are a few other threads on here that deal with assigning addresses to pointers directly, so you could check those out as well. 这里还有其他一些线程可以直接为指针分配地址,因此您也可以检查这些线程。 Here's one: How to initialize a pointer to a specific memory address in C++
这是一个: 如何在C ++中初始化指向特定内存地址的指针
You can do the task using std::istringstream
. 您可以使用
std::istringstream
来完成任务。 For example 例如
#include <iostream>
#include <sstream>
int main()
{
char bufferAddress[] = "0024ABC3";
std::istringstream is( bufferAddress );
void *p;
is >> p;
std::cout << "p = " << p << std::endl;
return 0;
}
The output is 输出是
p = 0x24abc3
If the buffer has type char *
then you can reinterpret this pointer to void to pointer to char. 如果缓冲区的类型为
char *
则可以将该指针重新解释为void,以指向char的指针。 For example 例如
char *buffer = reinterpret_cast<char *>( p );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.