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 342 views
1 answer 277 views
1 answer 218 views
1 answer 146 views
3 answers 1,615 views
1 answer 203 views
203 views asked Oct 8, 2014 by avibootz
3 answers 233 views
233 views asked May 14, 2021 by avibootz
...