如何在printf中查看结构的地址

Reg*_*ser 17 c struct pointers

我有一个返回地址的函数如下

struct node *create_node(int data)
{
        struct node *temp;
        temp = (struct node *)malloc(sizeof(struct node));
        temp->data=data;
        temp->next=NULL;
        printf("create node temp->data=%d\n",temp->data);
        return temp;
}
Run Code Online (Sandbox Code Playgroud)

struct node是哪里的

struct node {
        int data;
        struct node *next;
};
Run Code Online (Sandbox Code Playgroud)

如何在printf("")中查看存储在temp中的地址?

更新
如果我检查gdb中的地址,地址将以十六进制数格式显示,即0x602010,其中相同的地址printf("%p",temp)输入不同的数字,这与我在gdb打印命令中看到的不同.

jv4*_*v42 29

使用指针地址格式说明符%p:

printf("Address: %p\n", (void *)temp);
Run Code Online (Sandbox Code Playgroud)

  • +1:对于过于热心(迂腐正确)的编译器,将指针强制转换为`void*`:`printf("%p",(void*)temp)` (4认同)
  • @aroth:你不能保证`unsigned`和`struct node**`具有相同的表示形式:例如你的代码片段在64位机器上非常失败 (2认同)