C effective type is an access rule, not a layout guess
C’s effective-type rules constrain how an object’s stored value may be read. A pointer cast changes a pointer expression, not the type or representation of the object it points at.
A cast does not turn storage into a different object
Low-level C code often needs to inspect bytes, decode a wire format, or move data between representations. A tempting shortcut is to cast a pointer to an unrelated type and dereference it. The cast can produce a pointer value, but it does not by itself create an object of the pointed-to type, change an object’s effective type, or prove that the address meets the new type’s alignment requirement.
For an object with a declared type, the declared type is its effective type for accesses to its stored value. C11’s aliasing rule permits access through compatible types, corresponding signed or unsigned types, certain aggregates or unions containing them, and character types. For allocated storage without a declared type, the standard describes how a non-character store or a copy with memcpy can establish the effective type for later accesses. [1]
This is a rule about which lvalue may access a particular object. It is related to representation, but it is not a promise that two types have the same size, alignment, padding, or value encoding. Those are separate properties and may vary by implementation.
Character access is for bytes; typed access is for values
C permits an object representation to be inspected through a character type. That is the right tool when the question is “what bytes make up this object on this implementation?” It does not mean that an arbitrary byte sequence is automatically a valid value of every type. Reading an object representation as bytes and interpreting those bytes as a typed value are two different operations.
For example, copying the bytes of a float into an array of unsigned char is a portable way to examine the representation byte by byte. The resulting bytes are useful for diagnostics or for an implementation-specific format. They should not be treated as a portable network encoding: floating formats, byte order, padding, and the meaning of representations are not universally fixed by C. [1]
The reverse direction needs care too. A byte buffer received from a file or network may be short, misaligned, or contain a representation that is not valid for the intended type. Copying bytes into a properly declared object avoids dereferencing an incorrectly aligned cast pointer, but the program must still define its byte order, field widths, validation, and conversion rules.
#include <stdio.h>
#include <string.h>
int main(void) {
float value = 1.0f;
unsigned char bytes[sizeof value];
memcpy(bytes, &value, sizeof bytes);
for (size_t i = 0; i < sizeof bytes; ++i) {
printf("%02x%s", (unsigned)bytes[i],
i + 1 == sizeof bytes ? "\n" : " ");
}
}Use copying or explicit decoding at representation boundaries
When the goal is to transfer the representation of an existing object, memcpy communicates that intent and avoids accessing the source through an incompatible lvalue. When the goal is to parse external bytes, decode fields explicitly: assemble integers with shifts or use a specified serialization format, then validate ranges before forming application values. The two tasks may look similar in code, but one copies a local implementation representation and the other defines a portable protocol.
Suppose a packet contains a four-byte unsigned big-endian counter. Copying those four bytes directly into a host uint32_t silently assumes the host byte order and that the packet’s field exactly matches that type’s representation. A decoder instead computes (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3] using an unsigned type wide enough for the result. The explicit operations make the wire format visible and reviewable. A separate check should establish that four input bytes are available before reading them.
For a same-process representation conversion between scalar types, copying through a temporary object can avoid an aliasing violation, but the resulting bit pattern may still be implementation-dependent or invalid for the destination type. If the application needs a numeric conversion, perform a numeric conversion. If it needs a standardized byte format, specify and implement that format.
Compiler flags are not a substitute for a valid access
GCC enables -fstrict-aliasing at -O2, -O3, and -Os. Under that option, it may optimize on the assumption that accesses follow the language’s aliasing rules; its documentation also describes the C union-member case and warns that taking an unrelated pointer, casting it, and dereferencing it is not made valid by wrapping the pointer in a union cast. [2]
A program that appears to work at -O0 can therefore fail after optimization, inlining, a compiler upgrade, or a seemingly unrelated change. Turning strict aliasing off can be a practical compatibility measure for legacy code, but it does not repair alignment, bounds, lifetime, or representation errors. It can also conceal the underlying contract from future maintainers.
When reviewing low-level code, identify the actual object, its declared or effective type, the lvalue used for each access, and the representation the code expects. Then compile with warnings and optimization enabled, and test on more than one target when portability matters. The goal is not to avoid every cast; it is to make the cast’s job explicit and keep object access within the language’s rules.