#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct w{
int x;
int y;
} p1,p2;
struct w change(struct w);
int main(int argc, char *argv[]) {
struct w p1={1,2};
p2=change(p1);
printf("%d-%d\n",p1.x,p1.y);
printf("%d-%d\n",p2.x,p2.y);
return 0;
}
struct w change(struct w q){
scanf("%d",&q.x);
scanf("%d",&q.y);
return q;
}
struct w change(struct w);
这句是声明(declare)一个函数,这个函数
声明了这个函数后,在 main 函数里就可以使用这个函数。
p2=change(p1);
之所以要声明后才能在main里面使用,是因为,change函数的实现放在了main函数后面:
int main(int argc, char *argv[]) {
struct w p1={1,2};
p2=change(p1); // 这里使用
...
}
// 这里定义
struct w change(struct w q){
...
}
因此,你在main函数里使用change,就需要在main之前做函数的前置声明。所以如果你把change函数的实现提到main函数之前,就不需要多一个前置声明。
struct w change(struct w q){
...
}
int main(int argc, char *argv[]) {
struct w p1={1,2};
p2=change(p1);
...
}
struct 是用于结构体的
但是你用的也不像是结构体的啊
你删掉那一句, 再编译, 看看出什么问题。
因为你在 main() 中用了 change() 函数, 就要先说明一下这个函数的定义。 所以先放在main() 的前面。
定义了一个返回值为名为w对结构体的函数