سؤال

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