문제

I'm writing my program and i need to get a pointer from c-string. For example, i have a string like "0x3b021e01" and as an output i want a legal pointer void *ptr == 0x3021e101.

I tried this approach:

char *addr = "0x3021e101"; // this address is just an example of 32-bit addr

uint32_t iaddr = from_ch_to_hex(addr); // iaddr == 0x3021e101

void *data_buf = (void *)iaddr; // data_buf = 0x3021e101

uint32_t reg_val = *(uint32_t *)data_buf;

But I get Segmentation fault. What's wrong here? How can i fix it?

도움이 되었습니까?

해결책

0x3b021e101 is an odd number, so you would expect alignment errors.

By the way, unless you know by other means that the address is valid, say by having a pointer to that address, why don't you just use that pointer?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top