小编cso*_*er5的帖子

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

来自下面结构的指针“ 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)

c

0
推荐指数
1
解决办法
2437
查看次数

标签 统计

c ×1