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,851 questions

51,772 answers

573 users

How to define set value and return nested struct from function in C

1 Answer

0 votes
#include <stdio.h>
 
struct point 
{ 
    int x; 
    int y; 
};
struct rectangle
{ 
    struct point p1; 
    struct point p2; 
};

struct point createpoint(int x, int y);
struct rectangle setpoints(struct point p1, struct point p2);

int main(void)
{
    struct point p1; 
    struct point p2; 
    struct rectangle r1;
    
    p1 = createpoint(1, 3);
    p2 = createpoint(30, 15);
    r1 = setpoints(p1, p2);
    
    printf("r1.p1.x = %d r1.p1.y = %d\n", r1.p1.x, r1.p1.y);
    printf("r1.p2.x = %d r1.p2.y = %d\n", r1.p2.x, r1.p2.y);
    
    return 0;
}

struct point createpoint(int x, int y) 
{ 
    struct point p; 
 
    p.x = x; 
    p.y = y; 
 
    return p; 
} 

struct rectangle setpoints(struct point p1, struct point p2) 
{ 
    struct rectangle r;
    
    r.p1.x = p1.x;
    r.p1.y = p1.y;
    
    r.p2.x = p2.x;
    r.p2.y = p2.y;
    
    return r;
}
 

/*
run:
 
r1.p1.x = 1 r1.p1.y = 3
r1.p2.x = 30 r1.p2.y = 15

*/

 



answered Nov 27, 2015 by avibootz

Related questions

1 answer 244 views
1 answer 150 views
1 answer 190 views
3 answers 1,569 views
1 answer 180 views
180 views asked Oct 8, 2014 by avibootz
1 answer 134 views
2 answers 271 views
...