When I tried to read data from memory into an enumeration through the following code, I got these strange results:
void read_memory (const unsigned, const unsigned, unsigned* const); enum {DATA_0, DATA_1, DATA_2} data; read_memory(base_addr, offset, &data);
I worked on this by introducing a temporary variable of type unsigned. But I would like to make sure why the previous method does not work.
First of all, I know that the standard does not require a specific width for enumeration types if all elements can be represented. In fact, 6.7.2.2 says that:
Each numbered type must be compatible with char, a signed integer type, or an unsigned integer type.
But since the original data read from memory is placed in char, I think this should not be a problem. Moreover, if I understand correctly, “compatible” means that you can use it as if it were of this type. In particular, objects of an enumerated type can be operands of bitwise shift operators. I also know that signing can be a problem, because we don’t know if the listing is signed or not. But, as far as I can tell, 0x0900 is not signed.
So where is the problem?
source share