Passing structures to function
In C, there are two main ways to pass a structure to a function:
- Pass by Value: Passing a copy of the structure, which doesn’t affect the original structure.
- Pass by Reference: Passing the address of the structure, allowing modifications to the original structure.
Let’s walk through both methods with examples.
Define the Structure
First, let's define a Student
structure that has a name, roll number, and marks:
#include <string.h>
struct Student {
char name[50];
int rollNumber;
float marks;
};
Passing Structure by Value
When we pass a structure by value, a copy of the structure is passed to the function. Any changes made inside the function won’t affect the original structure.
Example: Passing by Value
void displayStudent(struct Student s) {
printf("Name: %s\n", s.name);
printf("Roll Number: %d\n", s.rollNumber);
printf("Marks: %.2f\n", s.marks);
}
int main() {
struct Student student1 = {"Alice", 101, 88.5};
// Pass by value
displayStudent(student1);
return 0;
}
In this example:
displayStudent
takes aStudent
structure as a parameter.- A copy of
student1
is passed todisplayStudent
, so any changes inside the function won’t affectstudent1
inmain
.
void modifyStruct(struct Student s) {
s.marks = 100; // Modifies only the copy of the structure
}
int main() {
struct Student student1 = {"Alice", 101, 88.5};
modifyStruct(student1);
// student1.marks is still 88.5, not affected by modifyStruct
printf("%s %d %f",student1.name,student1.rollNumber,student1.marks);
}
Passing Structure by Reference
When we pass a structure by reference, we pass a pointer to the structure. This allows the function to modify the original structure since it has the address of the structure.
void updateMarks(struct Student *s, float newMarks) {
s->marks = newMarks; // Use arrow operator to access and modify marks
}
int main() {
struct Student student1 = {"Alice", 101, 88.5};
// Display original marks
printf("Original Marks: %.2f\n", student1.marks);
// Pass by reference
updateMarks(&student1, 95.0);
// Display updated marks
printf("Updated Marks: %.2f\n", student1.marks);
return 0;
}
In this example:
updateMarks
takes aStudent
pointer (struct Student *s
) as a parameter.- We use the arrow operator (
->
) to access and modifymarks
via the pointer. updateMarks(&student1, 95.0);
passes the address ofstudent1
to the function, allowing it to modifystudent1
directly.
Key Points
- Pass by Value: Safe but doesn’t modify the original structure. Useful when you don’t need to change the data.
- Pass by Reference: Efficient for large structures, and it allows modification of the original data. Use when changes need to reflect in the original structure.
Summary Table
Method | Syntax in Function Parameter | Effect |
---|---|---|
Pass by Value | void func(struct Student s) | Copies the structure, original unchanged |
Pass by Reference | void func(struct Student *s) | Passes address, original structure can be modified |
Example:Develop a function that takes the two dates as input and compares them. The function should return 1 if date1 is earlier than date2, return 0 if date1 is later than date2. Write code to display whether date1 is earlier, later, or the same as date2 based on the function’s result. ( University Question)
#include <stdio.h>
// Structure to store date
typedef struct {
int day;
int month;
int year;
} Date;
// Function to compare two dates
int compareDates(Date date1, Date date2) {
// Compare years
if (date1.year < date2.year)
return 1;
if (date1.year > date2.year)
return 0;
// Compare months
if (date1.month < date2.month)
return 1;
if (date1.month > date2.month)
return 0;
// Compare days
if (date1.day < date2.day)
return 1;
if (date1.day > date2.day)
return 0;
// Dates are equal
return -1;
}
int main() {
Date date1, date2;
// Input two dates
printf("Enter first date (DD MM YYYY): ");
scanf("%d %d %d", &date1.day, &date1.month, &date1.year);
printf("Enter second date (DD MM YYYY): ");
scanf("%d %d %d", &date2.day, &date2.month, &date2.year);
// Compare dates and display result
int result = compareDates(date1, date2);
if (result == 1)
printf("Date1 is earlier than Date2.\n");
else if (result == 0)
printf("Date1 is later than Date2.\n");
else
printf("Both dates are the same.\n");
return 0;
}
Comments
Post a Comment