How to pass and return a struct from a function in C

1 Answer

0 votes
#include <stdio.h>

typedef struct Point {
    double x, y;
} Point;

void add(const Point *p1, const Point *p2, Point *out) {
    out->x = (p1->x + p2->x);
    out->y = (p1->y + p2->y);
}

int main() {
    Point p1 = {
        .x = 3.14,
        .y = 7.25
    };
    Point p2 = {
        .x = 12,
        .y = 37
    };
    Point p3;
    
    add(&p1, &p2, &p3);
    
    printf("%.2lf %.2lf", p3.x, p3.y);
    
    return 0;
}



/*
run:

15.14 44.25

*/

 



answered Dec 27, 2020 by avibootz

Related questions

1 answer 337 views
1 answer 272 views
1 answer 214 views
1 answer 139 views
3 answers 1,611 views
1 answer 197 views
197 views asked Oct 8, 2014 by avibootz
3 answers 226 views
226 views asked May 14, 2021 by avibootz
...