在试图通过以下法典将记忆中的数据读成一卷时,我得出了这些重要成果:
void read_memory (const unsigned, const unsigned, unsigned* const);
/* ... */
enum {DATA_0, DATA_1, DATA_2} data;
read_memory(base_addr, offset, &data); //data = 0x0900
data >>= 8; //data = 0x7e000000
我在此工作时采用了一种临时的未签署类型。 但我很想知道,以前的方法为何不可行。
First of all, I m aware that the standard does not require a specific width for enum types, as long as all members can be represented. As a matter of fact, 6.7.2.2 states that:
Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type.
But since the raw data read from memory fits in a char, I think it should not be an issue. Moreover, if I understand correctly, "compatible" means that you can use it as if it were of such a type. In particular, objects of enumerated type can be operands of bitwise shift operators. I m also aware that signedness may be a problem as we don t know if enum are signed or not. But, as far as I can tell, 0x0900 does not appear to be signed.
因此,问题何在?