我有一个结构
\nstruct c\n{\n int *id;\n int type; \n\n} obj;\nRun Code Online (Sandbox Code Playgroud)\n如何打印obj.id指向什么?并且还obj->id指向一些 int 变量
我试过
\nprintf("%p\\n",obj.id);\nRun Code Online (Sandbox Code Playgroud)\n但上面打印了一些地址
\n和
\nprintf("%d\\n",obj.id);\nRun Code Online (Sandbox Code Playgroud)\n在上面的编译器给出警告
\nformat \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\nRun Code Online (Sandbox Code Playgroud)\n
由于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)