#include <stdio.h>
//A byte pointer refers to a sequence of bytes
typedef unsigned char * byte_pointer;
//The input is a byte pointer and a byte number
void show_bytes(byte_pointer start,size_t len){
size_t i;
for (i = 0; i < len; i++) {
//A storage unit holds one byte
printf("%.2x",start[i]);
//Output in hexadecimal format with at least two digits
}
printf("\n");
}
void show_int(int x) {
show_bytes((byte_pointer) &x, sizeof(int));
}
void show_float(float x) {
show_bytes((byte_pointer) &x, sizeof(float));
}
void show_pointer(void *x) {
show_bytes((byte_pointer) &x, sizeof(void *));
}
void test_show_bytes(int val) {
int ival = val;
float fval = (float)ival;
int *pval = &ival;
show_int(ival);
show_float(fval);
show_pointer(pval);
}
int main() {
int t = 12345;
test_show_bytes(t);
return 0;
}
机器为 intel x86-64处理器:
在windows上:
(1)编译器x86,得到的地址为4字节
(2)编译器x64,得到的地址为8字节
同一台机子上的linux虚拟机:
这里我们使用的样例数字为12345 = 0x00003039 float ——0x4640e400
intel 是小端法机器,linux64使用8字节地址