[英]Why does the following print what it does?
typedef unsigned char byte;
unsigned int nines = 999;
byte * ptr = (byte *) &nines;
printf ("%x\n",nines);
printf ("%x\n",nines * 0x10);
printf ("%d\n",ptr[0]);
printf ("%d\n",ptr[1]);
printf ("%d\n",ptr[2]);
printf ("%d\n",ptr[3]);
Output: 输出:
3e7
3e70
231
3
0
0
I know the first two are just hexadecimal representations of 999 and 999*16. 我知道前两个只是999和999 * 16的十六进制表示形式。 What do the remaining 4 mean?
剩下的4是什么意思? the ptr[0] to ptr[3]?
ptr [0]到ptr [3]?
Most likely you are running this on a 32 bit LE system 999
in hex is:- 您最有可能在十六进制的32位LE系统
999
上运行此命令:
00 00 03 E7
- The way it would be stored in memory would be 00 00 03 E7
将其存储在内存中的方式为
E7 03 00 00
Hence:- E7 03 00 00
因此:-
ptr[0]
points to the byte containing E7
which is 231 in decimal ptr[0]
指向包含E7
的字节,该字节为十进制231
ptr[1]
points to the byte containing 03
which is 3 in decimal ptr[1]
指向包含03
的字节(十进制为3)
ptr[2]
points to the byte containing 00
which is 0 in decimal ptr[2]
指向包含00
的字节,十进制为0
ptr[3]
points to the byte containing 00
which is 0 in decimal ptr[3]
指向包含00
的字节,十进制为0
HTH! HTH!
I think that you will see clearly if you write: 我认为,如果您撰写以下内容,将会清楚地看到:
typedef unsigned char byte;
main() {
unsigned int nines = 999;
byte * ptr = (byte *) &nines;
printf ("%x\n",nines);
printf ("%x\n",nines * 0x10);
printf ("%x\n",ptr[0]);
printf ("%x\n",ptr[1]);
printf ("%x\n",ptr[2]);
printf ("%x\n",ptr[3]);
printf ("%d\n",sizeof(unsigned int));
}
char is 8 bits, one byte, and int is 4 bytes (in my 64 bytes machine). char是8位,一个字节,而int是4字节(在我的64字节机器中)。 In your machine the data is saved as little-endian so less significative byte is located first.
在您的计算机中,数据被保存为little-endian,因此,最不重要的字节被首先定位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.