如何从结构中打印指针的值

cso*_*er5 0 c

来自下面结构的指针“ p”中包含的值打印错误,我找不到正确打印它的方法。正确的代码是什么?代码:

#include <stdio.h>

struct my_struct{ //structure definition
    int a,*p;
};

int main(){

    my_struct var;
    var.a = 5;                  //variable definition
    var.p = &(var.a);           //pointer gets address from variable
    printf("%d\n",var.p);       // the number 2686744 is printed instead of the number '5'

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

dbu*_*ush 5

%d格式说明printf预计的int,但你传递一个int *

您需要取消引用指针以获取int

printf("%d\n",*(var.p));
Run Code Online (Sandbox Code Playgroud)