user2974730
user2974730

Reputation:

How to extract address from c-string

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?

Upvotes: 0

Views: 225

Answers (1)

Charlie Burns
Charlie Burns

Reputation: 7044

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?

Upvotes: 1

Related Questions