gpt4 book ai didi

c - 将结构传递给 C 中的函数

转载 作者:太空宇宙 更新时间:2023-11-04 07:13:10 25 4
gpt4 key购买 nike

#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 函数返回结构,因为这是一个小结构。

struct point get()
{
struct point p;
printf("Enter the x and y coordinates of your point: ");
scanf("%d %d",&p.x,&p.y);
return p;
}

int main ()
{
put(get());
return 0;
}

关于c - 将结构传递给 C 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26791024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com