Why does C allow accessing object using "character type":
6.5 Expressions (C)
An object shall have its stored value acc
The use of a character type to inspect the representations of objects is a hack. However, it is historical, and some accommodation must be made to allow it.
Mostly, in programming languages, we want strong typing. Something that is a float
should be accessed as a float
and not as an int
. This has a number of benefits, including reducing human errors and enabling various optimizations.
However, there are times when it is necessary to access or modify the bytes of an object. In C, this was done through character types. C++ continues that tradition, but it improves the situation slightly by eliminating the use of signed char
for these purposes.
Ideally, it might have been better to create a new type, say byte
, and to allow byte access to object representations only through this type, thus separating the regular character types only for use as normal integers/characters. Perhaps it was thought there was too much existing code using char
and unsigned char
to support such a change. However, I have never seen signed char
used to access the representation of an object, so it was safe to exclude it.