Modify the Data in a Structure by Passing a Pointer-to-Structure to a Function - C Structure

C examples for Structure:Structure Value

Description

Modify the Data in a Structure by Passing a Pointer-to-Structure to a Function

Demo Code

#include <stdio.h>

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

void doubleIt(struct rectangle *ptr);

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);
    
     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);
    
     doubleIt(&rect2);
    
     printf("after modification: \n");
     printf("height: %d\n", rect2.height);
     printf("width: %d\n\n", rect2.width);
    
     return(0);
}

void doubleIt (struct rectangle *ptr)
{
      ptr->height = 2 * ptr->height;
      ptr->width = 2 * ptr->width;
      return;
}

Result


Related Tutorials