I’m writing an interpreter and I’d like to be able to store whatever value a function returns into a void pointer. I’ve had no problem storing ints and various pointers as void pointers but I get an error when trying to cast a double as a void pointer. I understand that doubles are stored differently than integers and pointers at the bit level, but I don’t understand why I can’t place whatever bits I want into the pointer (assuming it has enough memory allocated) and then take them out later, casting them as a double.
Is it possible to cast a double to a void pointer using syntax I’m not aware of or am I misunderstanding how void pointers work?
On many systems a
doubleis 8 bytes wide and a pointer is 4 bytes wide. The former, therefore, would not fit into the latter.You would appear to be abusing
void*. Your solution is going to involve allocating storage space at least as big as the largest type you need to store in some variant-like structure, e.g. aunion.