Chinaunix首页 | 论坛 | 博客
  • 博客访问: 429400
  • 博文数量: 55
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1584
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-04 15:15
个人简介

热衷技术,热爱交流

文章分类

全部博文(55)

文章存档

2014年(7)

2013年(48)

分类: LINUX

2013-05-26 12:19:33

来源:《深入理解计算机系统第二版》

点击(此处)折叠或打开

  1. [~/c/csapp/code/data]#vi show-bytes_2.c
  2. #include <stdio.h>


  3. typedef unsigned char *byte_pointer; /*将byte_pointer定义为指向类型为“unsigned char”的指针变量*/


  4. void show_bytes(byte_pointer start, int len)
  5. {
  6.     int i;
  7.     for (i = 0; i < len; i++)
  8.         {printf(" %.2x ", start[i]); /*利用数组表示法引用指针,整数一次以两个数字的十六进制数输出*/
  9.         printf(" %p", &start[i]); /*打印指针变量指向的第i字节的地址*/
  10.         }
  11.     printf("\n");
  12. }
  13. void main()
  14. {
  15.       int val = 0x12345678;
  16.       byte_pointer valp = (byte_pointer) &val; /*强制把整形变量转换为byte_pointer*/
  17.       show_bytes(valp, 1); /* A. */
  18.       show_bytes(valp, 2); /* B. */
  19.       show_bytes(valp, 3); /* C. */
  20.       show_bytes(valp, 4); /* D. */
  21.       show_bytes(valp, 5); /* E. */
  22. }
  23. ~
  24. "show-bytes_2.c" 23L, 519C 已写入

                         
[~/c/csapp/code/data]#gcc -o show-bytes_2.so  show-bytes_2.c 

[~/c/csapp/code/data]#./show-bytes_2.so 

 78   0x7fff50aca8d4
 78   0x7fff50aca8d4 56   0x7fff50aca8d5
 78   0x7fff50aca8d4 56   0x7fff50aca8d5 34   0x7fff50aca8d6
 78   0x7fff50aca8d4 56   0x7fff50aca8d5 34   0x7fff50aca8d6 12   0x7fff50aca8d7
 78   0x7fff50aca8d4 56   0x7fff50aca8d5 34   0x7fff50aca8d6 12   0x7fff50aca8d7 d4   0x7fff50aca8d8
 
 结果显示,低字节位对应低地址位。所以,当前使用的linux是小端字节序。
阅读(2104) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~