C 指针转换

Sin*_*ash 3 c pointers typecasting-operator

#include <stdio.h>
int main(void)
 {
  char *cp;
  short *sp;
  int *ip;
  short x[6];
  int i, y;
  y = 0x0102;
  for (i = 0; i < 6; i++) 
  {
    x[i] = y;
    y = y + 0x1010;
  }
  cp = (char*) x;
  printf("1) *cp = %x\n", *cp);
  sp = x;
  printf("2) *sp = %x\n", *sp);
  printf("3) cp[3] = %x\n", cp[3]);
  ip = (int*) x;
  ip = ip + 1;
  printf("A) *ip = %x\n", *ip);
  printf("B) cp[6] = %x\n", cp[6]);
  sp = sp + 5;
  printf("C) *sp = %x\n", *sp);
  *x = *cp + 2;
  printf("D) cp[1] = %x\n", cp[1]);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我不明白如何将短数组类型转换为字符,以及类型转换发生时会发生什么。有人可以帮我解决这个问题!!

J..*_*..S 6

您声明了一个short名为 6 个元素的数组x。

您为每个元素分配了值。初始值将是:

+------+--------+
| x[0] | 0x0102 |           
+------+--------+
| x[1] | 0x1112 |             
+------+--------+             
| x[2] | 0x2122 |             
+------+--------+             
| x[3] | 0x3132 |             
+------+--------+             
| x[4] | 0x4142 |             
+------+--------+             
| x[5] | 0x5152 |             
+------+--------+
Run Code Online (Sandbox Code Playgroud)

我在这里做了一些假设,但如果不是你的情况,请修改它们。

sizeof(char)  = 1 bytes
sizeof(short) = 2 bytes
sizeof(int)   = 4 bytes
Endianness    = little endian
Run Code Online (Sandbox Code Playgroud)

所以在内存中,它看起来像

+----+----+----+----+----+----+----+----+----+----+----+----+
| 11 | 10 | 9  | 8  | 7  | 6  | 5  | 4  | 3  | 2  | 1  | 0  |
+----+----+----+----+----+----+----+----+----+----+----+----+
| 51 | 52 | 41 | 42 | 31 | 32 | 21 | 22 | 11 | 12 | 01 | 02 |
+----+----+----+----+----+----+----+----+----+----+----+----+
| x[5]    | x[4]    | x[3]    | x[2]    | x[1]    | x[0]    |
+---------+---------+---------+---------+---------+---------+
Run Code Online (Sandbox Code Playgroud)

哪里

  • 第一行:字节数(此处仅用于识别目的)
  • 第 2 行:内存中的 16 进制内容(每 2 个 16 进制字符代表一个字节)
  • 第三行:数组的索引 x

您转换了 的基地址x并将其分配给char指针cp。在printf()看到一个字符*x的大小是1字节。

x指向 byte 0 等,cp但在后者的情况下,只有1byte 被视为sizeof(char)is 1。

字节 0 的内容恰好是0x02.

所以,

cp = (char*) x;
printf("1) *cp = %x\n", *cp);
Run Code Online (Sandbox Code Playgroud)

打印2。

sp是一个short指针,与x. 所以*sp会打印与*x. 它将考虑2给定地址的第一个字节。sp指向字节0。所以字节0和1将被考虑。因此,该值是0x0102由

sp = x;
printf("2) *sp = %x\n", *sp);
Run Code Online (Sandbox Code Playgroud)

cp仍然指向 byte 0。cp[3]是有效的*(cp+3)。既然cp是一个char指针,cp+3将指向0 + 3*sizeof(char)这是0 + 3*1这又是一个字节3。

在字节3,我们0x11只有一个字节被视为sizeof(char)IS 1。

所以,

printf("3) cp[3] = %x\n", cp[3]);
Run Code Online (Sandbox Code Playgroud)

将打印 11

在

ip = (int*) x;
ip = ip + 1;
printf("A) *ip = %x\n", *ip);
Run Code Online (Sandbox Code Playgroud)

整数指针ip被分配了 的基地址x。然后ip增加1。ip最初指向字节 0。因为ip+1将指向字节0 + 1*sizeof(int),而0 + 1*4后者又是字节 4。

所以ip现在指向字节4。由于它是一个整数指针,因此考虑了4个字节——即字节4、5、6和7,其内容为31322122。


cp[6]是指*(cp+6)。cp+6指向字节 6,其内容为32.

因此,

printf("B) cp[6] = %x\n", cp[6]);
Run Code Online (Sandbox Code Playgroud)

打印32。

sp = sp + 5;
printf("C) *sp = %x\n", *sp);
Run Code Online (Sandbox Code Playgroud)

sp添加5到它之后,现在指向字节0 + 5*sizeof(short),0 + 5*2这又是字节 10,考虑到 2 个字节(字节 10 和 11)后的内容是5152.

*x = *cp + 2;
Run Code Online (Sandbox Code Playgroud)

*cp的价值是0x02。
*cp + 2是0x02 + 0x02= 0x04= 0x0004。
该值被分配给*x(字节 0 和 1)指向的整数。

所以字节 0 是现在0x04,字节 1 是现在0x00。

cp[1]是*(cp+1)。cp+1指向字节 1 内容现在0x00是打印时

printf("D) cp[1] = %x\n", cp[1]);
Run Code Online (Sandbox Code Playgroud)

已经完成了。