Your code is correct (any type can be aliased by unsigned char
). Also, on 2's complement systems, this alias is the same as the result of a value conversion.
The reverse operation; aliasing unsigned char
by char
is only a problem on esoteric systems that have trap representations for plain char
.
I don't know of any such systems ever existing, although the C standard provides for their existence. Unfortunately a cast is required because of this possibility, which is more annoying than useful IMHO.
The aliasing of unsigned char
by char
is the same as the value conversion on every modern system that I know of (technically implementation-defined, but everyone implements it that the value conversion retains the same representation).
NB. definition of terms, taking for example unsigned char x = 250;
:
- alias
char y = *(char *)&x;
- conversion
char y = x;