A cast is an explicit C conversion construct: it requests that an expression be treated as a specified type. For pointers, this can discard type information and prevent the compiler from detecting an incompatible target type. section 12.5
Inspecting an object byte by byte
unsigned val = 0xAABBCCDD; unsigned char* valp = (unsigned char*)&val; for (size_t i = 0; i < sizeof val; ++i) printf("byte[%zu]: 0x%.02hhX\n", i, valp[i]); Casting from an object pointer to a character-type pointer is described as mostly harmless and can expose the object's representation. The result still depends on the platform's representation, such as byte order. section 12.5 section 12.0
Treat casts as exceptional operations rather than routine repairs:
- Prefer compatible pointer types and carefully chosen types so the compiler can retain useful type information. section 12.5
- Do not use a cast to silence a diagnostic when the pointed-to object has a different size or interpretation; the access can cause serious damage. section 12.5
- Remember that a syntactically valid cast can hide an error because an untyped pointer no longer identifies the object type to the compiler. section 12.5
unsigned val = 0xAABBCCDD;
unsigned char* valp = (unsigned char*)&val;
for (size_t i = 0; i < sizeof val; ++i)
printf("byte[%zu]: 0x%.02hhX\n", i, valp[i]);
View the complete byte-inspection listing that demonstrates a cast from an unsigned object pointer to an unsigned-character pointer. section 12.5




















