如何在C中打印指针成员指向的值

Ale*_*lex -1 c

我有一个结构

\n
struct c\n{\n    int *id;\n    int type;   \n\n} obj;\n
Run Code Online (Sandbox Code Playgroud)\n

如何打印obj.id指向什么?并且还obj->id指向一些 int 变量

\n

我试过

\n
printf("%p\\n",obj.id);\n
Run Code Online (Sandbox Code Playgroud)\n

但上面打印了一些地址

\n

\n
printf("%d\\n",obj.id);\n
Run Code Online (Sandbox Code Playgroud)\n

在上面的编译器给出警告

\n
format \xe2\x80\x98%d\xe2\x80\x99 expects argument of type \xe2\x80\x98int\xe2\x80\x99, but argument 2 has type \xe2\x80\x98int *\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n

Ted*_*gmo 7

由于obj.id是指向(an ) 的指针,因此您需要取消引用它(使用运算符)。intint**

完整示例:

#include <stdio.h>

struct c {
    int *id;
    int type;   
} obj;

int main() {
    int x = 10;
    obj.id = &x;
    printf("%d\n", *obj.id);
}
Run Code Online (Sandbox Code Playgroud)