Re: memory address represented as a string

Glynn Clements <[email protected]>
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <[email protected]>
Saurabh Sehgal wrote:

> I had a quick question:
> 
> Let's say I design a function with the signature:
> 
> void * foo( char * addr ) ; ,
> 
> where addr is a string that represents a valid memory address  ...
> so the way someone can call this function is ...
> 
> char * addr = "0xae456778" // assume this is a valid memory address on
> the machine
> foo( addr ) ;
> 
> Is it possible to take this address in string form, and assign it to
> an actual pointer of void * type ?
> I want the function "foo" to return a pointer pointing to the memory
> location as indicated
> by the string passed in.

Yes:

	const char *addr = "0xae456778";
	void *ptr;

	sscanf(addr, "%p", &ptr);

The string needs to be in the format used by printf("%p"), which is
platform-specific.

Alternatively:

	ptr = (void *) strtoull(addr, NULL, 16);

However, strtoull() isn't in C89, although it's in C99 and POSIX.

Using strtoul() will work if sizeof(long) >= sizeof(void *). AFAIK,
this is true on all versions of Linux, but may not be true on some
other 64-bit platforms. On particular, it isn't true on Win64, where
"long" is only 32 bits.

-- 
Glynn Clements <[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.