How to pass structure through function in C programming?
In C program structure can be passed through function in two ways:
- Passing by value
- Passing by reference
Passing the structure as a value
Structures can be passed as arguments to functions like normal variables.
If a structure is passed as a value through a function, changes to the structure variable within the function definition will not affect the original structure variable.
Example: C program to create a structure named student, which has student’s name and roll number as members of the structure and use display() function to display the data.
#include
struct student
{
char name[50];
int roll;
};
void display(struct student stu);
// The function prototype should be below the structure declaration otherwise the compiler may throw an error.
int main()
{
struct student stud;
printf("Enter student's name: ");
scanf("%s", &stud.name);
printf("Enter roll number:");
scanf("%d", &stud.roll);
display(stud); //Pass the structure variable p stud as an argument.
return 0;
}
void display(struct student stu){
printf("Output\nName: %s",stu.name);
printf("\nRoll: %d",stu.roll);
}
output
Enter student's name: Tamim
Enter roll number: 5
Output
Name: Tamim
Roll: 5
Passing the struct by reference
When the structure is passed through the function as a reference, the memory address of the structure variable is passed through the function.
If the structure is passed as a reference through the function, changes to the structure variable within the function definition will also affect the original structure variable.
C program to add two distances in feet-inches and display the result without using a return statement.
#include
struct distance
{
int feet;
float inches;
};
void add(struct distance d1,struct distance d2,struct distance *d3);
int main()
{
struct distance dist1, dist2, dist3;
printf("First distance\n");
printf("Enter feet: ");
scanf("%d", &dist1.feet);
printf("Enter inch: ");
scanf("%f", &dist1.inch);
printf("Second distance\n");
printf("Enter feet: ");
scanf("%d", &dist2.feet);
printf("Enter inch: ");
scanf("%f", &dist2.inch);
add(dist1, dist2, &dist3);
/* Pass the dist1 and dist2 structure variables as values and the dist3 structure variable as a reference through the function */
printf("\nSum of distances = %d\'-%.1f\"", dist3.feet, dist3 .inch);
return 0;
}
void add(struct distance d1,struct distance d2, struct distance *d3)
{
// add distances d1 and d2 and store them in d3
d3->feet = d1.feet + d2.feet ;
d3->inch = d1.inch + d2.inch;
if (d3->inch >= 12) { /* If the inch value is greater than 12, it will be converted to feet. */
d3->inch -= 12;
+ +d3->feet;
}
}
output
First distance
Enter feet: 24
Enter inch: 7.8
Second distance
Enter feet: 7
Enter inch: 5.6
Sum of distances = 32'-1.4"
- In the above program the structure variables dist1 and dist2add are passed as values through the function. Because dist1 and dist2 need not be displayed in the main() function.
- But dist3 is passed as reference. That is, the address of dist3(&dist3) is passed as an argument to the ( ) function.
- addFor this reason , calling the function from the main function points to the address of d3 (structure pointer variable) dist3 add (structure variable) within the function . So any change in the d3 variable is reflected in the dist3 variable of the main function.
- As a result the correct sum is displayed in the output.