how to see address of a structure in printf

Viewed 42092

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.

3 Answers

EDIT: Don't do this! It prints the address of the pointer, not what you want!

I had all kinds of trouble getting this to work, but here's something that the compiler (I use the simple "cc" unix command line) didn't complain about and seemed to give appropriate results:

struct node temp;
// ... whatever ...
printf ("the address is %p", &temp);

[Rather than deleting, I left this as an example of what NOT to do. -smb]

enter code here

#include<stdio.h>
struct anywhere
{ 
double a;
int b;
char c;
float d;
}g;
int main()
{
printf("%p\n%p\n%p\n%p\n",&g.a,&g.b,&g.c,&g.d);
return 0
}        

now we can get output: address of g.a vice versa

we can print the structure variable address like this way and also how padding happening in the structures we can see by printing the address of the each and every member.

thank you all any mistakes and suggestions please ping comment .

Related