如何在printf中查看结构的地址
我有一个函数返回地址如下
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 node是哪里的
struct node { int data; struct node *next; };
如何在printf(“”)中查看存储在temp中的地址?
UPDATE
如果我检查gdb中的地址,则地址将以hex数格式显示,即0x602010,其中printf("%p",temp)
相同地址的数字与我在gdb print命令中看到的数字不同。
使用指针地址格式说明符%p
:
printf("Address: %p\n", (void *)temp);
编辑: 不要这样做! 它打印指针的地址,而不是你想要的!
我在使这个工作时遇到了各种麻烦,但是编译器(我使用简单的“cc”unix命令行)没有抱怨并且似乎给出了适当的结果:
struct node temp; // ... whatever ... printf ("the address is %p", &temp);
[而不是删除,我把这作为不做的例子。 -SMB]