Reputation: 105
If you have the memory address of a buffer, and this address is stored in a char, for example:
char bufferAddress[] = "0024ABC3"
, how can you create a pointer using bufferAddress
so that you can access variables in the buffer?
Upvotes: 1
Views: 2369
Reputation: 310980
You can do the task using 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. For example
char *buffer = reinterpret_cast<char *>( p );
Upvotes: 3
Reputation: 1333
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++
Upvotes: 3