#include "stdafx.h"

#include <stdio.h>

int main()
{
 unsigned int a = 0xFFFFFFF7;
    unsigned char i = (unsigned char)a;

 char *b = (char *)&a;

 printf("%08x,%08x",i,*b);

 getchar();
 return 0;
}

 

输出结果为:000000f7,fffffff7

分析:unsigned int 变量赋值给unsigned char 3个字节将会被截断为1字节(3位和高于3位的将被程序自动丢弃)。

第二个数,等价于 unsigned int *p = &a; char *b = (char*)p;

这句的意思是将unsigned int 型的指针强制转换为char型的指针。注意:这是char类型的指针转换,而不是char类型的转换。

这样转换的结果为

p+1 = x + 1*sizeof(int) = x + 1*4 =x+4;

b+1 = x+1*sizeof(char) = x + 1;

影响的是指针的寻址。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-18
  • 2021-12-25
  • 2021-07-24
  • 2021-11-18
  • 2021-08-27
猜你喜欢
  • 2021-11-28
  • 2021-05-26
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2019-11-07
  • 2022-12-23
相关资源
相似解决方案