Structures as Function Arguments

C is a structured programming language and the basic concept in it is the modularity of the programs. This concept is supported by the functions in C language. Let us look into the techniques of passing the structures to the functions.

This can be achieved in primarily two ways: Firstly, to pass them as simple parameter values by passing the structure name and secondly, through pointers. We will be concentrating on the first method in this unit and passing using pointers will be taken up in the next unit. Like other data types, a structure can be passed as an argument to a function. The program listing given below shows how to do this. It uses a function to display data on the screen.

Write a program to demonstrate passing a structure to a function.

/*Program to demonstrate passing a structure to a function.*/
#include <stdio.h>
/*Declare and define a structure to hold the data.*/
struct data{
float amt;
char fname [30];
char lname [30];
} per;
void print_per (struct data x);
printf(“Enter the donor’s first and last names separated by a space:”);
scanf (“%s %s”, per.fname, per.lname);
printf (“\nEnter the amount donated in rupees:”);
scanf (“%f”, &amp;per.amt);
print_per (per);
return 0;
void print_per(struct data x)
printf ("\n %s %s gave donation of amount Rs.%.2f.\n", x.fname, x.lname, x.amt);


Enter the donor’s first and last names separated by a space: RAVI KANT
Enter the amount donated in rupees: 1000.00
RAVI KANT gave donation of the amount Rs. 1000.00.

You can also pass a structure to a function by passing the structure’s address (that is, a pointer to the structure which we will be discussing in the next unit). In fact, in the older versions of C, this was the only way to pass a structure as an argument. It is not necessary now, but you might see the older programs that still use this method. If you pass a pointer to a structure as an argument, remember that you must use the indirect membership operator (–>) to access structure members in the function.

Please note the following points with respect to passing the structure as a parameter to a function.

• The return value of the called function must be declared as the value that is being returned from the function. If the function is returning the entire structure then the return value should be declared as the struct with the appropriate tag name.
• The actual and formal parameters for the structure data type must be the same as the struct type.
• The return statement is required only when the function is returning some data.
• When the return values of type is struct, then it must be assigned to the structure of the identical type in the calling function.

Let us consider another example as shown below, where structure salary has three fields related to an employee, namely – name, no_days_worked and daily_wage. To accept the values from the user we first call the function get_data that gets the values of the members of the structure. Then using the wages function we calculate the salary of the person and display it to the user.

Write a program to accept the data from the user and calculate the salary of the person using the concept of functions.

/* Program to accept the data from the user and calculate the salary of the person*/
struct sal {
char name[30];
int no_days_worked;
int daily_wage; };
struct sal salary;
struct sal get_dat(struct); /* function prototype*/
float wages(struct); /*function prototype*/
float amount_payable; /* variable declaration*/
salary = get_data(salary);
printf(“The name of employee is %s”,;
printf(“Number of days worked is %d”,salary.no_daya_worked);
printf(“The daily wage of the employees is %d”,salary.daily_wage);
amount_payable = wages(salary);
printf(“The amount payable to %s is %f”,,amount_payable);
struct sal get_data(struct sal income)
printf(“Please enter the employee name:\n”);
printf(“Please enter the number of days worked:\n”);
printf(‘Please enter the employee daily wages:\n”);
float wages(struct)
struct sal amt;
int total_salary ;
total_salary = amt.no_days_worked * amt.daily_wages;


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: