【发布时间】:2016-01-01 06:21:17
【问题描述】:
我已经在结构体内部定义了整数指针。我想使用结构体指针来使用该成员指针。我的代码如下所示:
#include<stdio.h>
#include<stdlib.h>
struct abc
{
int *x;
};
int main()
{
struct abc *p = (struct abc*)malloc(sizeof(struct abc));
p->x = (int*)malloc(sizeof(int));
p->x = 10;
printf("The value is %d\n",p->x);
free(p);
}
现在我得到了符合我期望的输出。但是我在编译时收到了警告消息。警告消息是:
temp.c:14:7: warning: assignment makes pointer from integer without a cast [enabled by default]
temp.c:15:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
我也试过了,
*p->x = 10
printf("The value is %d\n",*p->x);
但它不起作用。
如何解决这个警告?
【问题讨论】:
-
*p->x = 10; printf("The value is %d\n",*p->x);
标签: c pointers data-structures