### Memory Map - Is It Possible To Create A Pointer That Points To Specific Address?

I am currently writing a program that requires me to find certain files in map based on their permissions and use "munmap" to unmap them. I have done this but munmap needs the address of the file, which I have stored in a char array in hexadecimal. Is there a way for me to create a pointer that points to the hexadecimal address I have stored in the char array? The only thing I can find is how to hard code it and I can't do this because each mapping is different.

P.S. - I'm looking for a solution in C, not C++!
Last edited on
 ``123`` ``````// if the address is 0x123456: int* ptr = (int*)(0x123456);``````

If the address is stored as text, you'll have to convert it to a number first. you can use sscanf or atoi for that.
Last edited on

the problem with using alpha to int is that hex stores characters
idk if this helps but in C if you know the beginning address you can step through the rest

char *aPtr=yourArray;
//has the beginning address of your array now you can step through it with pointer notation
//and store the characters into something that can hold the address

what datatype are we trying to store?
Thanks for the responses,

Since the address is in hexa, it contains letters as well. aoti and scanf seem to only convert numbers. Trying to use "munmap(2)" - the parameters are 1) a pointer to the starting address and 2) the length to be unmapped. I have the starting address, but it is stored as a hexadecimal in a char array (i.e 0x7fa2c0e77000). I'm essentially trying to find a way to create a pointer that points to the hexadecimal address stored in the char array, not the actual array itself.

Code Example.

char Memory_Start[15]; // contains 12 bit hexadecimal address with '0x' at start and is null terminated.

Trying to create a pointer that points to the hexadecimal address INSIDE the char array.
Last edited on
Figured it out, for those who see this,

1) convert it to long int using strtol (Use strtoul if you need an unsigned long int)
2) cast to pointer of the appropriate type

Example:

 ``123456789101112`` ``````#include #include int main(void) { char sAddr[] = "0x00ac0098"; unsigned int uAddr = strtoul(sAddr,NULL,0); void* pAddr = (void*) uAddr; printf("%p", pAddr); return 0; }``````
Last edited on
Topic archived. No new replies allowed.