c - How to make a pointer to a variable whose address is stored in a char array? -
i have program in have stored address of variable "var" in char array this:
int var = 10; //defined in somefile sprintf(varaddress,"%p",&var)
so varaddress
stores address of var
(e.g. "0x7fff22b823dc"
).
is there way can use varaddress
create pointer variable var
?
assuming format of string matches implementation-defined format supported %p
format conversion, can parse pointer follows:
int var = 10; // writing address string char buf[100]; sprintf(buf, "%p", (void*)&var); printf("address string: %s\n", buf); // reading address void *ptr; sscanf(buf, "%p", &ptr); int *vptr = ptr; printf("got address back: %p\n", (void*)vptr);
note unless value of pointer came running process itself, attempt @ dereferencing pointer undefined behavior. moreover, object pointer retrieve string must still exist, , type of pointer must match type of object. these constraints make hard envision use case in reading pointer string serve practical purpose.
Comments
Post a Comment