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.

Loading…

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:

#include
        struct Book
    {
                int page;
                double price;
        };
void display(int, double);
int main()
{
     struct Book bok;
    printf("\nEnter the Book records:\n");
    printf("\nWhat are the numebr of pages in book? ");
    scanf("%d", &bok.page);
    printf("\nWhat is the price of book? ");
    scanf("%ld", &bok.price);
   
        display(bok.page, bok.price);
    return 0;   
}
void display(int a, double b)
{

    printf("\nThe number of pages in the book are %d", a);
    printf("\nThe price of the book is %ld", b);
        
}

 

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:

#include

struct Employee
{
    char name[10];
    int id;
    double salary;
    char address[10];
};

void display(struct Employee emp);

void main()
{
    struct Employee emp;
    printf("\nEnter the Employee records:\n");
    printf("\nEmployee name:\n");
    scanf("%s", emp.name);
    printf("\nEnter Employee id.:\n");
    scanf("%d", &emp.id);
    printf("\nEnter the salary of the Employee: \n");
    scanf("%ld",&emp.id);
    printf("\nEnter the address of the Employee: \n");
    scanf("%s", emp.address);
    display(emp);
}

void display(struct Employee emp)
{
    printf("\nemployee name is %s", emp.name);
    printf("\nid is %d", emp.id);
    printf("\nsalary is %ld", emp.salary);
    printf("\naddress is %s", emp.address);
}

 

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

 

Loading…