Structure and functions in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Structures can be passed as function arguments like all other data types. We can pass individual members of a structure, an entire structure, or a pointer to a structure to a function. Like all other data types, a structure or a structure member or a pointer to a structure can be returned by a function. Structure-function helps in writing better code.

Introduction

Structure functions in C make the code efficient. A code that consumes less memory and takes less time to execute is good.

Before we jump into the concept of structure and functions in C. Let us go through some prerequisites.

Functions are reusable codes that perform a specific task when they are called.

Derived data types are formed from fundamental data types. Structures are one such user-defined data type. The structures can have many fundamental data types known as structure members grouped into a single user-defined data type.

Functions are the blocks of codes that perform a specific task when called. We need to pass the parameters to the function, and the function returns the result. Structures can also be passed as parameters to the functions.

When a function is called, if we pass the values of the variables to the function, it is known as the call by value. Instead of passing the values, if we pass the address of the variables to the function, it is known as call by reference.

The dot (.) operator is used to access a structure member. The arrow (->) operator is to access the members of a structure when the pointer references the structure.

With these basics of structures and functions, It will be easy to understand structure functions clearly.

Structure-function can be effectively used while writing code. Structures can be passed as arguments to the functions. This can be done in three ways. They are,

  • Passing the members of the structures as an argument.
  • Passing the entire structure as an argument.
  • Passing the address of the structure as arguments.

How to Pass Structure Members To Functions?

Sometimes we don’t want to pass the entire structure to the function. We want to pass only a few members of the structure. We can use the dot (.) operator to access the individual members of the structure and pass them to the function.

Let us create a structure to hold the details of a student, such as the name of the student, roll number, and marks, and print out just the roll number and marks using a function. Passing the entire structure to the function is unnecessary when we want to print only a few structure members.

In the above example, the structure contains the name of the student as well, but we need to print only the percentage and roll number. Therefore we pass only the required structure members to function.

Let us look at the code and understand how to pass structure members to the function.

In the above code, we created a structure to hold the name, roll number, and percentage of the student. The input from the user is stored in the structure. A function named display() is created, which takes the roll number and the percentage of the student as the parameter. Using the dot (.) operator, we accessed the member of the structure and passed it to the function.

The output of the above code is as follows:

In this way, we can pass structure members to a function.

How to Return Structure from a Function?

In our learning journey of structure-function, let's learn How to Return Structure from a Function. We are familiar with returning a variable from a function such as return 0, return a, etc. We can also return multiple variables in the form of a single structure variable.

Let us look at an example and understand how a structure is returned from a function.

Explanation

In the above code:

  • we created a structure named wage and a function named employee().
  • The structure wage will store the name and wage of an employee.
  • In the main() function, we called the employee() function which we defined.
  • The called function, i.e., the employee() function, asks the user to enter the name and wage of an employee, and it is stored in the structure named e1.
  • The called function returns the structure e1 to the main() function.
  • The structure members from the structure returned from the called function can be accessed using the dot (.) operator.
  • The structure members are then printed in the main() function.

The output of the above code is as follows:

In this way, we can return structure from a function and access the members of the returned structure.

How to Pass Structure by Reference

Next, in our journey of learning structure-function, let's learn How to Pass Structure by Reference.

Passing the parameter as a value will make a copy of the structure variable, passing it to the function. Imagine we have a structure with a huge number of structure members. Making a copy of all the members and passing it to the function takes a lot of time and consumes a lot of memory. To overcome this problem, we can pass the address of the structure.

Pointers are the variables that hold the address of other variables. We can use pointers to pass the structure by reference.

Let us look at an example to understand how to pass a structure using pointers.

Explanation

In the above code:

  • A structure named car and a function named print_struct() are defined. The structure stores the model name, seating capacity, and the fuel type of the vehicle.
  • In the main() function, we created a structure variable named tata and stored the values. Later the address of the structure is passed into the print_struct() function, which prints the details entered by the user.
  • The address is passed using the address operator ampersand (&). To access the pointer members, we use the arrow operator -> operator.

The output of the above code is as follows:

In this way, we can pass the address of the structure as a parameter to a function using pointers.

An Array of Structures as Function Arguments

An array is a collection of similar data types. We know that even a structure is a data type from previous knowledge. Therefore a group of structures of the exact definition is known as an array of structures.

Let us look at an example to understand how to pass an array of structures as function arguments.

Explanation:

In the above example:

  • a structure named details is created. The details structure has name, sec, and per as structure members, which store the name, section, and percentage of the student, respectively.
  • In the main() function, we created an array of structure variables named student.
  • A function named print_struct() is created. The print_struct() function prints the name, section, and percentage. The array student is passed to the print_struct() function. The print_struct() function accesses each element of the array one by one and prints the name, section, and percentage.

The output of the above function is as follows.

In this way, we can easily pass an array of structures as function arguments.

Conclusion

  • Functions are a set of reusable codes which perform a specified task on the parameters passed to them when they are called.
  • Structure-function can be used to write code effectively.
  • The structure can be passed as a parameter to the function.
  • An entire structure can be passed to a function, or individual members of the structure can be passed into the function.
  • Individual members of the structure can be accessed using the dot operator.
  • A structure can be returned from a function using the return keyword.
  • Structures can be passed into functions either by reference or by value.
  • An array of structures can also be passed to a function.

See Also: