如何删除此警告?
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);
}
j
在int*
类型中使用时:printf("%d",p1->j);
,printf
不喜欢它并想要一个int
所以你应该取消引用:
printf("%d",*(p1->j));
以下陈述
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
收益地址不值。