Which of the casts is allowing me to convert an
int*
to anunsigned char*
?
That C-style cast in this case is the same as reinterpret_cast<unsigned char*>
.
Can I cast any T* type like this?
Yes and no. The yes part: You can safely cast any pointer type to a char*
or unsigned char*
(with the appropriate const
and/or volatile
qualifiers). The result is implementation-defined, but it is legal.
The no part: The standard explicitly allows char*
and unsigned char*
as the target type. However, you cannot (for example) safely cast a double*
to an int*
. Do this and you've crossed the boundary from implementation-defined behavior to undefined behavior. It violates the strict aliasing rule.