the program is to decide big endian or little endian.
This is the answer given in the book:
int Test(){
short int word = 0x0001;
char *byte = (char *) &word;
return (byte[0] ? BIG:LITTLE);
}
I don’t understand this line: char *byte = (char *) &word; Does it mean “pass the address of word into byte”? So, now byte point to word’s original address? As I known, short int is 2 bytes. So, does “byte” point to higher address or lower address? Why?
How does this work?
It’s just taking the address of
word, casting it to a char pointer, and putting it inbyte.At that point,
bytewill point to the first byte of 2-byteword, and the value of that byte (1 or 0) will tell you if you’re on a big or little endian machine.