Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to see address of a structure in printf

Tags:

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

where struct node is

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

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

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.

like image 577
Registered User Avatar asked Jun 20 '11 10:06

Registered User


1 Answers

Use the pointer address format specifier %p:

printf("Address: %p\n", (void *)temp); 
like image 168
jv42 Avatar answered Sep 29 '22 23:09

jv42