Passing structures to functions in C language

Structures can be provided as parameters to functions in the C programming language. By enabling the passing of numerous values as a single parameter, improves the organization and efficiency of the code. A copy of the structure is formed when a structure is submitted to a function; any changes made to the copy do not impact the original structure.

Example:

#include <stdio.h>
#include <string.h>

struct student {
   char name[50];
   int age;
   float gpa;
};

void printStudent(struct student s) {
   printf("Name: %s\n", s.name);
   printf("Age: %d\n", s.age);
   printf("GPA: %f\n", s.gpa);
}

int main() {
   struct student s1;

   // Assigning values to structure members
   strcpy(s1.name, "John");
   s1.age = 20;
   s1.gpa = 3.8;

   // Passing structure to function
   printStudent(s1);

   return 0;
}

Output:

Name: John
Age: 20
GPA: 3.800000

In this example, we define a student structure with three members: name, age, and gpa. We then define a function printStudent that takes a student structure as its parameter and prints its members.

In the main function, we create a student structure s1 and assign values to its members. We then pass s1 as an argument to the printStudent function. The function creates a copy of the s1 structure and prints its members.