开发者

how to assign address to a pointer where address is store in CString object?

开发者 https://www.devze.com 2023-01-29 05:39 出处:网络
CString str(\"0x1b1be0\") void* ptr = NULL; Now i want to assign ptr =0x1b1be0开发者_如何学编程. How do I achieve this?Try this:
CString str("0x1b1be0")
void* ptr = NULL;

Now i want to assign ptr = 0x1b1be0开发者_如何学编程. How do I achieve this?


Try this:

CString str("0x1b1be0");
void* ptr = (void *)_tcstoul(str.GetBuffer(), 0, 0);

And this is in pure C:

#include <stdlib.h>
#include <stdio.h>

int main()
{
    char *s = "0x1b1be0";
    void *p = (void *)strtoul(s, 0, 0);
    printf("%p", p);
    return 0;
}

Try it on Codepad.

Note: This version has no error checking. Just to demonstrate the idea.


#include <sstream>

std::stringstream s;
s << "0x1234";
void* a;
s >> std::hex >> a;


Here is a code.

#include <sstream>
#include <iostream>

int main() {
    void * x;   
    std::stringstream ss;
    ss << std::hex << "0x1b1be0";
    ss >> x;
    std::cout << x << std::endl;
}


Hmmm, unless you're writing something very low level such as a debugger, having to do such a thing is highly questionable...

Are you sure you know what a pointer means ?

Do you know that pointers are private to your process ? They most often have no meaning outside.

To answer your question, you obviously have to:

  1. Convert your string to an intptr_t using for example std::sscanf with the %x format specifier.
  2. Assign your pointer with reinterpret_cast< void* >.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号