Menu
Coddy logo textTech

Passing Structs by Value

Part of the Logic & Flow section of Coddy's C journey — lesson 46 of 63.

Now that you know how to work with structs and pointers to structs, it's time to learn how to pass entire structs to functions. When you pass a struct to a function as a parameter, C uses pass-by-value, which means the function receives a complete copy of the struct.

Here's the syntax for a function that accepts a struct parameter:

void functionName(struct StructureName parameterName) {
    // Function body
}

Using our familiar Point struct as an example:

void printPoint(struct Point p) {
    printf("Point coordinates: (%d, %d)\n", p.x, p.y);
}

The key concept to understand is that any changes you make to the struct inside the function will not affect the original struct in the calling function. This is because the function works with its own copy of the data.

This approach is useful when you want to read or display struct data without risking accidental modifications to the original. However, keep in mind that copying large structs can be inefficient in terms of memory and performance.

challenge icon

Challenge

Easy

Create a C program that demonstrates passing structs to functions by value. Your program should:

  1. Define a struct named Rectangle with the following members:
    • An integer width to store the rectangle's width
    • An integer height to store the rectangle's height
    • A character array color with size 15 to store the rectangle's color
  2. Write a function named displayRectangle that:
    • Takes a Rectangle struct as a parameter (pass-by-value)
    • Prints the rectangle information in this exact format:
      • Rectangle Details:
      • Width: [width]
      • Height: [height]
      • Color: [color]
      • Area: [area]
    • Calculates and prints the area (width × height)
  3. Write a function named modifyRectangle that:
    • Takes a Rectangle struct as a parameter (pass-by-value)
    • Doubles both the width and height of the rectangle
    • Changes the color to "Modified"
    • Prints Inside modifyRectangle function:
    • Calls displayRectangle to show the modified rectangle
  4. In the main function:
    • Create a Rectangle variable named rect
    • Read the width, height, and color from input and assign them to the struct members
    • Print Original rectangle:
    • Call displayRectangle to show the original rectangle
    • Call modifyRectangle with the rectangle
    • Print After modifyRectangle call:
    • Call displayRectangle again to show that the original rectangle is unchanged

This challenge demonstrates the key concept of pass-by-value with structs: when you pass a struct to a function, the function receives a complete copy of the struct. Any modifications made inside the function affect only the copy, not the original struct in the calling function.

Cheat sheet

When passing structs to functions, C uses pass-by-value, meaning the function receives a complete copy of the struct.

Function syntax for accepting a struct parameter:

void functionName(struct StructureName parameterName) {
    // Function body
}

Example with a Point struct:

void printPoint(struct Point p) {
    printf("Point coordinates: (%d, %d)\n", p.x, p.y);
}

Key concept: Changes made to the struct inside the function do not affect the original struct in the calling function, since the function works with its own copy of the data.

This approach is useful for reading or displaying struct data without risking modifications to the original, but copying large structs can be inefficient.

Try it yourself

#include <stdio.h>
#include <string.h>

// TODO: Define the Rectangle struct here

// TODO: Write the displayRectangle function here

// TODO: Write the modifyRectangle function here

int main() {
    // Read input
    int width, height;
    char color[15];
    
    scanf("%d", &width);
    scanf("%d", &height);
    scanf("%s", color);
    
    // TODO: Create Rectangle variable and assign input values
    
    // TODO: Print "Original rectangle:" and call displayRectangle
    
    // TODO: Call modifyRectangle
    
    // TODO: Print "After modifyRectangle call:" and call displayRectangle again
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow