C Structure & Function

In this tutorial, you will learn about the process of passing the elements of structure into the function. The C language grants us permission to pass the structure elements as an argument to the function. We can pass the structure into the function by passing its element or passing the entire structure. Please refer to the following sites at first to make sure that you know all the basic concepts required for this topic.

C Programming Functions

C Structure

We can pass the structures in the function using either of the two ways. The two different ways for passing structure elements to the function are:

How can we pass the elements of structure to the function?

The C language allows us to pass the structure as an argument to the function. We can pass the structure as an individual member or a separate variable. Please see the following example to learn more about its implementation.

Code:

 

The above code will display its output as below.

Output:

Enter the Book records:
What is the number of pages in the book? 1400
What is the price of the book? 350
The number of pages in the book are 1400
The price of the book is 350

How can we pass the entire structure to the function?

There may even rise a case where the structure will have multiple elements on it. In such case of structures, passing all the individual elements will be a tedious task. For this reason, we will pass the whole structure to the function as a function argument. See the following example to learn more about its implementation details.

Code:

 

Hence, the above code will generate the output as below.

Output:

Enter the Employee records:
Employee name:
Jack
Enter Employee id.:
3
Enter the salary of the Employee:
45000
Enter the address of the Employee:
Florida
employee name is Jack
id is 45000
salary is 6487720
address is Florida

 

SHARE C Structure & Function

You may also like...

Leave a Reply

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

Share