简体   繁体   中英

Structures in functions

I am new to structures and I have a task to first make a structure "Points" that holds x and y (DONE) then make a function printPoint() that would print the points made in main (DONE) and then I must make a function Point createPoint(double x, double y) that would create a Point type structure, fill it in with coordinates and return it, so the p1 and p2 variables in main would be made with createPoint() , how do I exactly do that?

#include <stdio.h>
#include <stdlib.h>

typedef struct Point {
    int x;
    int y;
} Point;

void printPoint(Point p1, Point p2) {
    printf("%d %d\n%d %d", p1.x, p1.y, p2.x, p2.y);
}

Point createPoint(double x, double y) {
    ?
}

int main()
{
    Point p1 = {2.0, -3.0};
    Point p2 = {-4.0, 5.0};
    printPoint(p1, p2);
}

It's as simple as that:

Point createPoint(double x, double y) {
  Point point = {x, y};
  return point;
}
...
printPoint(createPoint(1,2), createPoint(3,4));
... 

But double should probably be int here. Or maybe struct Point should contain double fields rather than int fields. Only you know the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM