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
EasyCreate a C program that demonstrates passing structs to functions by value. Your program should:
- Define a
structnamedRectanglewith the following members:- An integer
widthto store the rectangle's width - An integer
heightto store the rectangle's height - A character array
colorwith size 15 to store the rectangle's color
- An integer
- Write a function named
displayRectanglethat:- Takes a
Rectanglestruct 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)
- Takes a
- Write a function named
modifyRectanglethat:- Takes a
Rectanglestruct 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
displayRectangleto show the modified rectangle
- Takes a
- In the main function:
- Create a
Rectanglevariable namedrect - Read the width, height, and color from input and assign them to the struct members
- Print
Original rectangle: - Call
displayRectangleto show the original rectangle - Call
modifyRectanglewith the rectangle - Print
After modifyRectangle call: - Call
displayRectangleagain to show that the original rectangle is unchanged
- Create a
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;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions