求解 C语言指针结构体报错+warning问题
编译器是gcc,用的是devC++,下面是源代码其中第17行报错:[Error]invalidtypeargumentofunary'*'(have'int')第18行警...
编译器是gcc,用的是dev C++,下面是源代码
其中
第17行报错: [Error] invalid type argument of unary '*' (have 'int')
第18行警告:[Warning] passing argument 1 of 'print' makes pointer from integer without a cast [enabled by default]
第21行报错:[Error] conflicting types for 'getStruct'
以下代码在Unix环境下运行正常
--------------------------------------------------------------------------------------------
#include <stdio.h>
struct point{
int x;
int y;
};
struct point* gerStruct(struct point *);
void output (struct point);
void print(const struct point *p);
int main(int argc,char const *argv[])
{
struct point y = { 0 , 0};
getStruct(&y);
output(y);
output(*getStruct(&y));
print(getStruct(&y));
}
struct point* getStruct(struct point *p)
{
struct point p;
scanf("%d",&p.x);
scanf("%d",&p.y);
printf("%d %d\n",p.x,p.y);
return p;
}
void output (struct point p)
{
printf("%d %d\n",p.x,p.y);
}
void print(const struct point *p)
{
printf("%d %d\n",p->x,p->y);
} 展开
其中
第17行报错: [Error] invalid type argument of unary '*' (have 'int')
第18行警告:[Warning] passing argument 1 of 'print' makes pointer from integer without a cast [enabled by default]
第21行报错:[Error] conflicting types for 'getStruct'
以下代码在Unix环境下运行正常
--------------------------------------------------------------------------------------------
#include <stdio.h>
struct point{
int x;
int y;
};
struct point* gerStruct(struct point *);
void output (struct point);
void print(const struct point *p);
int main(int argc,char const *argv[])
{
struct point y = { 0 , 0};
getStruct(&y);
output(y);
output(*getStruct(&y));
print(getStruct(&y));
}
struct point* getStruct(struct point *p)
{
struct point p;
scanf("%d",&p.x);
scanf("%d",&p.y);
printf("%d %d\n",p.x,p.y);
return p;
}
void output (struct point p)
{
printf("%d %d\n",p.x,p.y);
}
void print(const struct point *p)
{
printf("%d %d\n",p->x,p->y);
} 展开
展开全部
#include <stdio.h>
struct point{
int x;
int y;
};
struct point* getStruct(struct point *);
void output (struct point);
void print(const struct point *p);
int main(int argc,char const *argv[])
{
struct point y = { 0 , 0};
getStruct(&y);
output(y);
output(*getStruct(&y));
print(getStruct(&y));
}
struct point* getStruct(struct point *p)
{
//static struct point p;
scanf("%d",&p->x);
scanf("%d",&p->y);
printf("%d %d\n",p->x,p->y);
return p;
}
void output (struct point p)
{
printf("%d %d\n",p.x,p.y);
}
void print(const struct point *p)
{
printf("%d %d\n",p->x,p->y);
}
追问
第24-26行的改动,明白了,我把指针来当成结构来访问了。。。
但能解释一下第23行为什么不能声明struct point p呢,怎么成静态的了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询