This article refers to "in-depth understanding of computer systems" P31.
First look at the following code: 12345 hexadecimal representation: 0x00003039
1#include <stdio.h>2 3 intMain ()4 {5 intA =12345;6 Char*q = (Char*) (&a);7 for(inti =0; I <sizeof (a); ++i)8printf"%.2x", Q[i]);9 return 0;Ten}
The output is:
The address of A is the int* type, whose object is 4 bytes of type int, 12345, when it is cast to the unsigned char* type q, since char is 1 bytes, its object is split by one byte byte, because the memory is small end storage (low address, High address), so q[0] is 4 bytes of 12345 in memory of the first byte, and so on (12345 of the 4 bytes are forcibly split into 4 single-byte storage), so the final output is 39 30 00 00.
C language-print byte representations of different objects (int* cast to unsigned char* understanding)