Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

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 306 views
1 answer 246 views
1 answer 192 views
1 answer 118 views
3 answers 1,570 views
1 answer 180 views
180 views asked Oct 8, 2014 by avibootz
3 answers 195 views
195 views asked May 14, 2021 by avibootz
...