Passing Structure to Function in C Programming

You can pass a structure variable to a function as argument like we pass any other variable to a function. Structure variable is passed using call by value. We can also pass individual member of a structure as function argument.

C Program to Pass Structure Variable to Function

In below program, we are passing a member variable to printAge function and whole structure variable to printEmployeeDetails function.

#include <stdio.h>

struct employee {
 char name[100];
 int age;
 float salary;
 char department[50];
};

void printEmployeeDetails(struct employee emp){
   printf("Employee Details\n");
   printf(" Name : %s\n Age : %d\n Salary = %f\n Dept : %s\n", 
       emp.name, emp.age, emp.salary, emp.department);
}

void printAge(int age){
    printf("\nAge = %d\n", age);
}

int main(){
   struct employee employee_one, *ptr;
   
   printf("Enter Name, Age, Salary and Department of Employee\n");
   scanf("%s %d %f %s", &employee_one.name, &employee_one.age,
       &employee_one.salary, &employee_one.department);
   
   /* Passing structure variable to function */
   printEmployeeDetails(employee_one);
   
   /* Passing structure member to function */
   printAge(employee_one.age);

   return 0;
}
Output
Enter Name, Age, Salary and Department of Employee
Rose 35 1234.5 Purchase
Employee Details
 Name : Rose
 Age : 35
 Salary = 1234.500000
 Dept : Purchase
 
Age = 35