Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing string to pointer

Tags:

c++

c

pointers

Suppose I have the memory address as a string representation (say "0x27cd10"). How can I convert this to a pointer (void*)?

i.e.

int main() {
     const char* address = "0x29cd10";
     void* p;

     // I want p to point to address 0x29cd10 now...

     return 0;
}
like image 219
peter61 Avatar asked Dec 25 '10 16:12

peter61


1 Answers

strtol lets you specify the base (16, for hexadecimal, or 0 to auto-detect based on the 0x prefix in the input) when parsing the string. Once you have the pointer stored as an integer, just use reinterpret_cast to form the pointer.

like image 106
Ben Voigt Avatar answered Oct 02 '22 16:10

Ben Voigt