对于跨越多字节的程序对象,我们必须建立两个规则:这个对象的地址是什么,以及在存储器中如何排列这些字节。在几乎所有的机器中,多字节对象都被存储为连续的字节序列,对象的地址是所使用字节中的最小地址

     小端法:存储器中按照从最低有效字节到最高有效字节的顺序存储对象;

     大端法:存储器中按照从最高有效字节到最低有效字节的顺序存储对象;

    不同的机器采用不同的规则,我们可以通过程序来了解自己所用机器所采用的规则。

#include<stdio.h>

typedef unsigned char *byte_pointer;  //byte_pointer定义为指向unsigned char类型的对象指针

void show_bytes(byte_pointer start,int len){

int i;

for(i=0;i<len;i++)

 printf(" %.2x",start[i]);

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);

}

相关文章:

  • 2022-12-23
  • 2021-10-08
  • 2021-08-15
  • 2021-12-18
  • 2021-12-07
  • 2021-11-15
  • 2021-12-09
  • 2021-08-20
猜你喜欢
  • 2021-10-08
  • 2021-07-14
  • 2021-11-17
  • 2021-11-13
  • 2021-08-02
  • 2021-08-25
相关资源
相似解决方案