【发布时间】:2015-01-03 15:23:50
【问题描述】:
#include<stdio.h>
#include<stdlib.h>
struct point
{
int x;
int y;
};
void get(struct point p)
{
printf("Enter the x and y coordinates of your point: ");
scanf("%d %d",&p.x,&p.y);
}
void put(struct point p)
{
printf("(x,y)=(%d,%d)\n",p.x,p.y);
}
int main ()
{
struct point pt;
get(pt);
put(pt);
return 0;
}
我正在尝试编写一个程序来从用户那里获取 x 和 y 坐标,然后他们将它们打印到屏幕上。一旦我输入 x 和 y 坐标并将它们打印到屏幕上,我得到:(x,y)=(56,0)。我是使用结构的新手,所以任何帮助都会很好。谢谢。
【问题讨论】:
-
您需要通过引用传递结构或传递指向它的指针。目前,当您将其发送到 get 和 put 函数时,您正在复制整个结构。
标签: c structure pass-by-value