I’m coding something in C and dealing with situations where it would be helpful to be able to say “cast this to a pointer pointing to a datatype with the same number of bytes as this number”.
An example will make it clearer – I’m using size_t to get the size of a pointer on the system. Now I want to portably cast a void * pointer to a pointer to a datatype of that size. I know I could cast it to any pointer type and it would be the same length, but the pointer arithmetic differs depending on the datatype the variable is pointing to. Also, in my case size_t happens to be 8 bytes, and so does double, so I could just cast my void * to a double *, but I want a portable solution. Is there any way to do this?
What wrong about
size_t*(assuming I understood your question)In the general case, where you need pointer to a type with
nbytes, you can do: