If I have the following code:
#include <stdint.h>
union data_t {
int8_t sbyte;
uint8_t ubyte;
int16_t sint;
uint16_t uint;
int32_t slong;
uint32_t ulong;
int64_t sint64;
uint64_t uint64;
float qsingle;
double qdouble;
long double qfloat;
};
union data_t *data;
data = malloc(sizeof(union data_t));
data.uint = 2534;
Here, I have assigned the uint16_t type as 42, an I safely access a data type smaller than the one I have assigned to (such as uint8_t), and have it safely typecast (to 230)? This answer (Accessing inactive union member and undefined behavior?) seems to say that it is allowed in C11, but I'm unsure as to whether or not this is legal in C99.