First of all, you need to understand what this type of cast does: it reinteprets the object taking all data of a source object bit by bit. Second thing to understand is: all pointer objects are of the same size.
You can always convert between all pointers, and the cast itself will always work, but the validity of this operation depends on what you are going to do with the area of memory pointed by the pointer. In general case, it can give you access to some area behind the object pointed to be the original pointer. The result of such access can be different. In some cases, you can get just garbage. In other cases, you may access the object behind the memory area reserved for the original object, so you can write to this area (note that your pointers are not constant), corrupt those objects, cause General Protection Fault exception, anything.
Note all of those kind of trouble can happen even without the cast, if, by some other reason, your pointer points to some wrong area of memory.
Let's see:
- uint64_t* to uint32_t*
No problem, except loosing part of data for 64-bit integers which are bigger than maximum 32-bit integers, so you can get wrong data. (With negative data, the situation is the same, just more complicated; please see Two's complement — Wikipedia, the free encyclopedia.)
You cannot harm other data, because the new pointer, result of the case, gives you access to smaller area of memory than.
- uint64_t* -> unsigned char* -> uint32_t*
uint64_t* -> void* -> uint32_t*
It depends what you are doing. If you don't touch intermediate pointer objects (unsigned char*
, void*
), just cast it again, it makes no difference. This intermediate cast is simply redundant, makes no sense at all. You cannot access anything directly with void pointer unless you cast it again. As to pointer to a character, it points to even less area of memory than pointer to uint32
, so you cannot corrupt anything using it, by the reason I explained in item #1.
Note that I never mentioned the purpose of all these manipulations, so I'm an not sure anything of that makes no sense. The question about "correctness" of these operations does not make any predefined sense; it all depends on what you try to achieve and what you are going to do with the results of the case. The most important thing is that you have to understand what really happens.
—SA