Method 1
Idea: Use Pointer to force type conversion
Copy codeThe Code is as follows: # include <stdio. h>
Int main (void)
{
Int data1 = 0x12345678;
Int I;
For (I = 0; I <4; I ++)
{
Printf ("% # x -----> % p \ n", * (char *) & data1 + I), (char *) & data1 + I );
}
Return 0;
}
From the output results, we can see that the high address 0xbfc1b1ff stores 0x12 of the data, and the low address 0xbfc1b1fc stores 0x78 of the data. Therefore, it is a small client. We can also see that the data address actually points to the space for storing low-level data.
Method 2
Idea: Use the shared body to store all data from the same address.
Copy codeThe Code is as follows: # include <stdio. h>
Int main (void)
{
Int I;
Union endian
{
Int data;
Char ch;
} Test;
Test. data = 0x12345678;
If (test. ch = 0x78)
{
Printf ("little endian! \ N ");
}
Else
{
Printf ("big endian! \ N ");
}
For (I = 0; I <4; I ++)
{
Printf ("% # x ------- % p \ n", * (char *) & test. data + I), (char *) & test. data + I );
}
Return 0;
}