多个链接的指针

tom*_*ole 1 c pointers

对于家庭作业,我必须构建以下简单方案。

在此处输入图片说明

我的尝试看起来像:

#include <stdlib.h>

int main() {
  char* heap1P = malloc(sizeof(char**));
  char* heap2P = malloc(sizeof(char*));
  char* heap3P = malloc(sizeof(char));

  *heap3P = 'X';
  *heap2P = heap3P;
  *heap1P = heap2P;

  char*** stackP = heap1P;

  puts("stack                           | heap ");
  printf("%p [%p] | %p [%p] => %p [%p] => %c [%p] \n", stackP, &stackP, *heap1P, heap1P, *heap2P, heap2P, *heap3P, heap3P);

  return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

首先,我在内存中分配空间,然后设置值。输出类似于(格式:值[地址]):

stack                           | heap 
0x55a1e184f260 [0x7fff05e55c08] | 0xffffff80 [0x55a1e184f260] => 0xffffffa0 [0x55a1e184f280] => X [0x55a1e184f2a0] 
Run Code Online (Sandbox Code Playgroud)

如您所见,堆栈值包含第一个堆值的地址。但是堆值不正确。它们不包含以下堆值的地址。

为什么堆值不包含给定的地址?

Ser*_*sta 5

问题仅在于您已将各种指针声明为char *。它看起来似乎并不重要,因为在常见的实现中,所有指针都具有相同的表示形式。但是,一旦取消引用它们,它就变得至关重要!

让我们看一下以下语句:

*heap3P = 'X';
*heap2P = heap3P;
Run Code Online (Sandbox Code Playgroud)

第一个是正确的:heap3P是,char **heap3P分配了char,在这里一切都很好。

第二个太可怕了。由于heap2Pchar *heap3P如果转换成整数,并修剪成char!长话短说:您只存储指针中的一个字节...如果仔细看一下这些值,您会发现不同heapx的确实是单字节值...

解决方法很简单:

char*** heap1P = malloc(sizeof(char**));
char** heap2P = malloc(sizeof(char*));
char* heap3P = malloc(sizeof(char));
Run Code Online (Sandbox Code Playgroud)

并且代码在没有警告的情况下编译并按预期运行!