C 指针报告不同值的副本

C 指针报告不同值的副本,c,pointers,C,Pointers,我正在用c语言建立游戏的项目系统 我想使用头来存储项数组的地址, 当此程序运行时,标头将返回 相应的地址 这是我的密码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> struct Header{ struct ItmInfo *itmAddress; // a pointer that store the address of ItmI

我正在用c语言建立游戏的项目系统

我想使用头来存储项数组的地址, 当此程序运行时,标头将返回 相应的地址

这是我的密码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

struct Header{
struct ItmInfo *itmAddress;
// a pointer that store the address of ItmInfo array
...//some variables
}Head;

struct ItmInfo{
...    //some variables
}Itm;  //struct for Item

int main()
{
    struct Header *head=malloc(1*sizeof (*head)); //open a header array, currently it have only 1 member 
    struct ItmInfo *itm= malloc (memStack*sizeof(*itm));// open a item array
 
    head[0].itmAddress = itm; //copy item array address in to the header

    printf ("Address of item list is %p\n",*itm);
    printf ("Address in header is %p\n",head[0].itmAddress);

    return 0;
}
我做错了什么,一开始这样做可以吗??,谢谢

 printf ("Address of item list is %p\n",*itm);
应该是

printf ("Address of item list is %p\n", (void *)itm);

取消引用指针并将整个结构推送到堆栈(它可以是兆字节的数据,如本例中的80.000.000字节的数据:)。这是未定义的行为

您希望显示存储在itm指针中的引用

    printf ("Address of item list is %p\n", (void *)itm);
    printf ("Address in header is %p\n", (void *)head[0].itmAddress);


如果您将转换为
void*
printf(“项目列表地址为%p\n”,(void*)*itm)
永远不会编译

*itm
itm
不是一回事,所以它当然会打印不同的值。更合适的是,它应该是
printf(“项目列表的地址是%p.\n”,(void*)itm)。指针应强制转换到
void*
,以便使用
%p
打印。现在不同类型的指针有不同的表示是不常见的,但是C标准允许它,并且说
%p
的参数应该有type
void*
。此外,我还插入了句号,因为句子应该以句号(或问号或感叹号)结尾。@EricPostpischil,我很惊讶(*itm)竟然打印出任何东西。至少在VS中我得到了一个警告。
    printf ("Address of item list is %p\n", (void *)itm);
    printf ("Address in header is %p\n", (void *)head[0].itmAddress);