Build a Structure Using a Function which accepts values of individual members of structure, builds a structure, and returns it - C Structure

C examples for Structure:Structure Value

Description

Build a Structure Using a Function which accepts values of individual members of structure, builds a structure, and returns it

Demo Code

#include <stdio.h>

struct rectangle {
 int height;/*from w  ww  . j  a va2 s.  c o m*/
 int width;
};

struct rectangle makeIt(int height, int width);

int main()
{
     struct rectangle rect1, rect2;
     rect1 = makeIt(20, 30);
     rect2 = makeIt(40, 80);
    
     printf("rect1: \n");
     printf("height: %d\n", rect1.height);
     printf("width: %d\n\n", rect1.width);
    
     printf("rect2: \n");
     printf("height: %d\n", rect2.height);
     printf("width: %d\n\n", rect2.width);
    
     return(0);
}

struct rectangle makeIt(int height, int width)
{
      struct rectangle myRectangle;
      myRectangle.height = height;
      myRectangle.width = width;
      return myRectangle;
}

Result


Related Tutorials