I have a struct
struct c
{
int *id;
int type;
} obj;
how to print what obj.id
pointing to? and also point obj->id
to some int variable
I tried
printf("%p\n",obj.id);
but above is printing some address
and
printf("%d\n",obj.id);
in above compiler gives warning
format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’
CodePudding user response:
Since obj.id
is a pointer to an int
(an int*
) you need to dereference it (using the *
operator).
Full example:
#include <stdio.h>
struct c {
int *id;
int type;
} obj;
int main() {
int x = 10;
obj.id = &x;
printf("%d\n", *obj.id);
}
CodePudding user response:
You need to dereference the pointer:
printf("%d\n",*(obj.id));