Modify the Data in a Structure by Passing It to a Function which accepts a structure as an input argument and returns a structure after modifying data in it - C Structure

C examples for Structure:Structure Value

Description

Modify the Data in a Structure by Passing It to a Function which accepts a structure as an input argument and returns a structure after modifying data in it

Demo Code

#include <stdio.h>

struct rectangle {
 int height;//from w w  w  . ja  v a 2 s.c  o m
 int width;
};

struct rectangle doubleIt(struct rectangle ourRect);

int main()
{
     struct rectangle rect1 = {10, 15}, rect2 = {25, 35};
    
     printf("before modification: \n");
     printf("height: %d\n", rect1.height);
     printf("width: %d\n\n", rect1.width);
    
     rect1 = doubleIt(rect1);
    
     printf("after modification: \n");
     printf("height: %d\n", rect1.height);
     printf("width: %d\n\n", rect1.width);
    
     printf("before modification: \n");
     printf("height: %d\n", rect2.height);
     printf("width: %d\n\n", rect2.width);
    
     rect2 = doubleIt(rect2);
     printf("after modification: \n");
     printf("height: %d\n", rect2.height);
     printf("width: %d\n\n", rect2.width);
    
     return(0);
}

struct rectangle doubleIt (struct rectangle ourRect)
{
      ourRect.height = 2 * ourRect.height;
      ourRect.width = 2 * ourRect.width;
      return ourRect;
}

Result


Related Tutorials