Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to pass an entire structure as an argument to function in C?
In C programming, structures can be passed to functions in three ways. Here we'll focus on passing an entire structure as an argument to a function, where the complete structure variable is passed by value.
Syntax
return_type function_name(struct structure_name variable_name);
When passing an entire structure as an argument −
- The structure variable name is given as argument in the function call
- It is collected in another structure variable in the function header
- A complete copy of the structure is created, which uses additional memory
Example 1: Basic Structure Passing
This example demonstrates how to pass a date structure to a display function −
#include <stdio.h>
struct date {
int day;
char month[10];
int year;
};
void display(struct date d) {
printf("Day: %d<br>", d.day);
printf("Month: %s<br>", d.month);
printf("Year: %d<br>", d.year);
}
int main() {
struct date d = {18, "January", 2021};
printf("Date Information:<br>");
display(d); // passing entire structure as argument
return 0;
}
Date Information: Day: 18 Month: January Year: 2021
Example 2: Structure for Arithmetic Operations
Here's an example that passes a structure containing two numbers to perform addition −
#include <stdio.h>
struct numbers {
int var1;
int var2;
};
void calculate(struct numbers nums) {
int sum = nums.var1 + nums.var2;
int product = nums.var1 * nums.var2;
printf("Sum: %d<br>", sum);
printf("Product: %d<br>", product);
}
int main() {
struct numbers n = {25, 15};
printf("Numbers: %d and %d<br>", n.var1, n.var2);
calculate(n); // passing structure by value
return 0;
}
Numbers: 25 and 15 Sum: 40 Product: 375
Example 3: Student Grade Calculation
This example shows passing a student structure to calculate total marks −
#include <stdio.h>
struct student {
char name[20];
int subject1;
int subject2;
int subject3;
};
void displayResult(struct student s) {
int total = s.subject1 + s.subject2 + s.subject3;
float average = total / 3.0;
printf("Student: %s<br>", s.name);
printf("Marks: %d, %d, %d<br>", s.subject1, s.subject2, s.subject3);
printf("Total: %d<br>", total);
printf("Average: %.2f<br>", average);
}
int main() {
struct student s1 = {"Alice", 85, 92, 78};
displayResult(s1); // entire structure passed by value
return 0;
}
Student: Alice Marks: 85, 92, 78 Total: 255 Average: 85.00
Key Points
- Memory Usage: Passing by value creates a complete copy of the structure
- Safety: Changes inside the function don't affect the original structure
- Performance: For large structures, this method can be memory-intensive
Conclusion
Passing entire structures as function arguments is straightforward but creates copies in memory. This method is safe as original data remains unchanged, but consider using pointers for large structures to improve efficiency.
