I'm trying to convert any variable to a void pointer and store the address as a number so I can later have access to the variable using the number as ID.
Here's my code:
#include <stdio.h>
int main()
{
int num = 123456789;
void *ptr = #
unsigned long addr = *(unsigned long *)ptr;
printf("addr: %lu\n", addr);
ptr = (void *)addr;
num = (int)ptr;
printf("num: %d\n", num);
}
Result:
addr: 17275796522765372693
num: 123456789
Is this a correct and safe way to do it? Will it also work with non-primitive variable types like struct?