How to pass structure through function in C programming?

How to pass structure through function in C programming?

How to pass structure through function in C programming?
In this chapter you will see some examples of passing structures as arguments to functions and learn how to use them in your programs.

How to pass structure through function in C programming?

In C program structure can be passed through function in two ways:

  1. Passing by value
  2. 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 <stdio.h>
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 <stdio.h>
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"
				
			

Leave a Comment

Your email address will not be published. Required fields are marked *