意外的编译器警告

问题描述 投票:-3回答:2

如何删除此警告?

warning: format '%d' expects argument of type ' int', but argument 2 has type 'int *' [-Wformat=] printf(“%d”,p1->j);

这是代码,一切正常,除了警告。

void main()
{
    struct s1
    {
        int *j;
    };

    struct s2
    {
        int k;
    };
    struct s1 *p1;
    struct s2 *p2;

    p1=malloc(sizeof(struct s1));
    p2=malloc(sizeof(struct s2));
    p2->k=5;
    p1->j=&p2->k;
    printf("%d",p1->j);
}
c format-specifiers
2个回答
1
投票

jint*类型中使用时:printf("%d",p1->j);printf不喜欢它并想要一个int所以你应该取消引用:

printf("%d",*(p1->j));

1
投票

以下陈述

p1->j=&p2->k; /* check the operator precedence */

应该

p1->j=&(p2->k); /* j is type of ptr, it should hold address of k variable */

同时访问printf("%d",p1->j); - > printf("%d",*(p1->j));因为p1->j收益地址不值。

© www.soinside.com 2019 - 2024. All rights reserved.