如何在printf中查看结构的地址 [英] how to see address of a structure in printf

查看:140
本文介绍了如何在printf中查看结构的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a function which returns address as following

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;
}

struct节点位于

where struct node is

struct node {
        int data;
        struct node *next;
};

如何在printf(")中看到临时存储的地址?

How can I see in printf("") the address stored in temp?

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

UPDATE
If I check the adressed in gdb the addresses are coming in hex number format i.e. 0x602010 where as same address in printf("%p",temp) is coming in a different number which is different from what I saw in gdb print command.

推荐答案

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

Use the pointer address format specifier %p:

printf("Address: %p\n", (void *)temp);

这篇关于如何在printf中查看结构的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆