Structures and Functions in C

Like all other types, we can pass structures as arguments to a function. In fact, we can pass, individual members, structure variables, a pointer to structures etc to the function. Similarly, functions can return either an individual member or structures variable or pointer to the structure.

Let’s start with passing individual member as arguments to a function.

Passing Structure Members as arguments to Function

We can pass individual members to a function just like ordinary variables.

The following program demonstrates how to pass structure members as arguments to the function.

Expected Output:

How it works:

In lines 7-12, a structure student is declared with three members namely name, roll_no and marks.

In line 14, a prototype of function print_struct() is declared which accepts three arguments namely name of type pointer to char, roll_no of type int and marks is of type int.

In line 18, a structure variable stu of type struct student is declared and initialized.

In line 19, all the three members of structure variable stu are passed to the print_struct() function. The formal arguments of print_struct() function are initialized with the values of the actual arguments.

From lines 25-27, three printf() statement prints name, roll_no and marks of the student.

The most important thing to note about this program is that stu.name is passed as a reference because name of the array is a constant pointer. So the formal argument of print_struct() function i.e name and stu.name both are pointing to the same array. As a result, any changes made by the function print_struct() will affect the original array. We can verify this fact by making the following amendments to our program.

  1. In the main function add the following line after the call to print_struct() function.
  2. In print_struct() function add the following two lines just before the last printf() statement.

Now our program should look like this:

Expected Output:

This verifies the fact that changes made by print_struct() function affect the original array.

Passing Structure Variable as Argument to a Function

In the earlier section, we have learned how to pass structure members as arguments to a function. If a structure contains two-three members then we can easily pass them to function but what if there are 9-10 or more members ? Certainly passing 9-10 members is a tiresome and error-prone process. So in such cases instead of passing members individually, we can pass structure variable itself.

The following program demonstrates how we can pass structure variable as an argument to the function.

Expected Output:

How it works:

In lines 7-12, a structure student is declared with three members namely: name, roll_no and marks.

In line 14, the prototype of function print_struct() is declared which accepts an argument of type struct student.

In line 18, a structure variable stu of type struct student is declared and initialized.

In line 19, print_struct() function is called along with argument stu. Unlike arrays, the name of structure variable is not a pointer, so when we pass a structure variable to a function, the formal argument of print_struct() is assigned a copy of the original structure. Both structures reside in different memory locations and hence they are completely independent of each other. Any changes made by function print_struct() doesn’t affect the original structure variable in the main() function.

printf() statements from lines 25-27 prints the details of the student.

Passing Structure Pointers as Argument to a Function

Although passing structure variable as an argument allows us to pass all the members of the structure to a function there are some downsides to this operation.

  1. Recall that a copy of the structure is passed to the formal argument. If the structure is large and you are passing structure variables frequently then it can take quite a bit of time which make the program inefficient.
  2. Additional memory is required to save every copy of the structure.

The following program demonstrates how to pass structure pointers as arguments to a function.

Expected Output:

How it works:

In lines 7-13, a structure employee is declared with four members namely name, age, doj(date of joining) and designation.

In line 15, the prototype of function print_struct() is declared which accepts an argument of type pointer to struct student.

In line 19, a structure variable dev of type struct employee is declared and initialized.

In line 20, print_struct() is called along with along with the address of variable dev. The formal argument of print_struct() is assigned the address of variable dev. Now ptr is pointing to the original structure, hence any changes made inside the function will affect the original structure.

The printf() statements from lines 27-30 prints the details of the developer.

The downside of passing structure pointer to a function is that the function can modify the original structure. If that is what you intentionally want that’s fine. However, if don’t want functions to modify original structure use the const keyword. Recall that const keyword when applied to a variable makes it read-only. Let’s rewrite the previous program using const keyword.

Now even though we are passing a structure pointer to print_struct() function, any attempt to modify the values of the structure will result in compilation error. Try commenting out code in line 32 and see it yourself.

Array of Structures as Function Arguments

We have already seen how to pass an array of integers to a function. Similarly, we can pass an array of structures to a function.

The following program demonstrates how we can pass an array of structures to a function.

Expected Output:

How it works:

In lines 7-13, a structure company is declared with four members namely name, ceo, revenue, pps.

In line 15, the prototype of function print_struct() is declared which accepts an argument of type array of structures.

In lines 20-24, an array of structure called companies of type struct company is declared and initialized.

In line 25, print_struct() is called along with argument companies. Since the name of the array is a constant pointer to the 0th element of the array, the formal argument of print_struct() is assigned the address of variable companies. So now str_arr is pointing to the original array of structure, any changes made inside the function will affect the original structure. If you don’t want to call a function to modify the original structure use the keyword const.

In line 32, variable i is declared to control the for loop.

In lines 34-41, a for loop is used to loops through the array of structure and prints the details of each company.

The control then passes to the main() function and the function terminates.

The formal arguments of print_struct() can also be declared as follows:

But why ?

Recall that name of the array i.e companies is a constant pointer to the 0th element of the array. In this case, 0th element is of type struct company. So the type of companies is a pointer to struct company or (struct company*). That’s why str_arr is declared as a pointer to struct company or (struct company*).

Returning Structure from Function

Just as we can return fundamental types and arrays, we can also return a structure from a function. To return a structure from a function we must specify the appropriate return type in the function definition and declaration. Consider the following example:

This function accepts an argument of type struct player and returns an argument of type struct player.

The following program demonstrates how we can return a structure from a function.

Expected Output:

How it works:

In lines 7-13, a structure of type player is declared with 4 members namely name, height, weight and fees.

In line 15, the prototype of print_struct() is declared which accepts an argument of type struct player and returns nothing.

In line 16, the prototype of deduct_fees() is declared which accepts an argument of type struct player and returns a structure of type struct player.

In line 20, a structure variable p of type struct player is declared and initialized.

In line 21, the print_struct() function is passed an argument of type struct player. The function prints the details of the player and passes the control back to main() function.

In line 22, deduct_fees() function is called with an argument of type struct player. The function decrements the fees of the player by 1000 using the statement.

and then returns the structure variable p to the called function i.e main(), where it is assigned back to the variable p.

In line 23, the print_struct() is called again with the same argument as before to check whether the details have been modified by deduct_fees() or not.

After printing the details of the function the control passes back to main() function and the program terminates.

Returning a Structure Pointer from Function

In the last section, we have learned that a function can return a structure variable. So it should be no surprise that it can also return a pointer to structure variable.

To return structure pointer from a function all we need to do is to specify the appropriate return type in the function definition and function declaration. For example:

This function accepts an argument of type pointer to struct movie and returns and a pointer of type struct movie.

The following program demonstrates how we can return structure pointers from a function.

Expected Output:

How it works:

In lines 7-14, a structure movie is declared with 5 members namely title, language, director, year and rating.

In line 16, the prototype of function print_struct() is declared which accepts an argument of type pointer to struct movie and returns nothing.

In line 17, another prototype of function add_rating() is declared which accepts an argument of type pointer to struct movie and also returns a pointer of type struct movie.

In line 21, a struct variable m of type struct movie is declared and initialized.

In line 22, two pointer variables ptr_m1 and ptr_m2 of type struct movie are declared and ptr_m1 is assigned the address of m.

In line 24, print_struct() is called to print the details of the movie.

In line 25, add_rating() function is called along with the address of variable m. The function modifies the value of rating and returns the pointer to the called function where it is assigned to a pointer variable ptr_m2.

In line 26, print_struct() is called again but this time ptr_m2 is passed to it. After printing the details the control is transferred back to main() and the program terminates.

Leave a Comment