小编Ale*_*xey的帖子

关于演员的指针算术

我目前正在参加CS107课程,该课程做出以下假设:

  • sizeof(int) == 4
  • sizeof(short) == 2
  • sizeof(char) == 1
  • 大字节

我的教授展示了以下代码:

int arr[5];
((short*)(((char*) (&arr[1])) + 8))[3] = 100;
Run Code Online (Sandbox Code Playgroud)

这是代表的20个字节arr:

|....|....|....|....|....|
Run Code Online (Sandbox Code Playgroud)

我的教授&arr[1]在这里指出了一点,我同意这一点.

|....|....|....|....|....|
     x
Run Code Online (Sandbox Code Playgroud)

我现在明白,(char*)使指针成为char(1字节)的宽度而不是int的宽度(4字节).

我不明白的是+ 8,我的教授在此指出:

|....|....|....|....|....|
                         x
Run Code Online (Sandbox Code Playgroud)

但是它不应该指向这里,因为它的前进是char(1字节)大小的8倍?

|....|....|....|....|....|
               x
Run Code Online (Sandbox Code Playgroud)

c casting pointer-arithmetic

10
推荐指数
1
解决办法
535
查看次数

标签 统计

c ×1

casting ×1

pointer-arithmetic ×1