这两天开始看,《深入理解计算机系统》,目前的阶段我很适合看这本书。
这是书上的例题,按单个字节输出。
也在考察内存是大端存储还是小端存储。
#include <stdio.h>
typedef unsigned char *byte_pointer; // 声明一个字节指针
void show_bytes(byte_pointer start, int len)
{
int i = 0;
for (int i = 0; i < len; i++)
{
printf("%2.x",start[i]);
}
printf("\n");
}
void show_float(int x)
{
show_bytes((byte_pointer)&x, sizeof(float));
}
void show_pointer(void *x)
{
show_bytes((byte_pointer)&x, sizeof(void *));
}
void show_int(int x)
{
show_bytes((byte_pointer)&x, sizeof(int));
}
int main()
{
int val = 0x87654321;
byte_pointer valp = (byte_pointer)&val;
show_bytes(valp, sizeof(int));
system("pause");
return 0;
}