Passing and Return Structure form Function in C

Advertisements

Structure and Function in C

Using function we can pass structure as function argument and we can also return structure from function.

Passing structure as function argument

Structure can be passed to function through its object therefore passing structure to function or passing structure object to function is same thing because structure object represents the structure. Like normal variable, structure variable(structure object) can be pass by value or by references / addresses.

Passing Structure by Value

In this approach, the structure object is passed as function argument to the definition of function, here object is reperesenting the members of structure with their values.

Example for passing structure object by value


       #include<stdio.h>

       struct Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;
       };

       void Display(struct Employee);
       void main()
       {
              struct Employee Emp = {1,"Kumar",29,45000};

              Display(Emp);

       }

       void Display(struct Employee E)
       {
                    printf("\n\nEmployee Id : %d",E.Id);
                    printf("\nEmployee Name : %s",E.Name);
                    printf("\nEmployee Age : %d",E.Age);
                    printf("\nEmployee Salary : %ld",E.Salary);
       }

   Output :

              Employee Id : 1
              Employee Name : Kumar
              Employee Age : 29
              Employee Salary : 45000

				

Passing Structure by Reference

In this approach, the reference/address structure object is passed as function argument to the definition of function.

Example for passing structure object by reference


       #include<stdio.h>

       struct Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;
       };

       void Display(struct Employee*);
       void main()
       {
              struct Employee Emp = {1,"Kumar",29,45000};

              Display(&Emp);

       }

       void Display(struct Employee *E)
       {
                    printf("\n\nEmployee Id : %d",E->Id);
                    printf("\nEmployee Name : %s",E->Name);
                    printf("\nEmployee Age : %d",E->Age);
                    printf("\nEmployee Salary : %ld",E->Salary);
       }

   Output :

              Employee Id : 1
              Employee Name : Kumar
              Employee Age : 29
              Employee Salary : 45000

				

Function Returning Structure

Structure is user-defined data type, like built-in data types structure can be return from function.

Example for passing structure object by reference


       #include<stdio.h>

       struct Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;
       };

       Employee Input();            //Statement   1
       void main()
       {
              struct Employee Emp;

              Emp = Input();

              printf("\n\nEmployee Id : %d",Emp.Id);
              printf("\nEmployee Name : %s",Emp.Name);
              printf("\nEmployee Age : %d",Emp.Age);
              printf("\nEmployee Salary : %ld",Emp.Salary);

       }

       Employee Input()
       {
              struct Employee E;

                    printf("\nEnter Employee Id : ");
                    scanf("%d",&E.Id);

                    printf("\nEnter Employee Name : ");
                    scanf("%s",&E.Name);

                    printf("\nEnter Employee Age : ");
                    scanf("%d",&E.Age);

                    printf("\nEnter Employee Salary : ");
                    scanf("%ld",&E.Salary);

              return E;             //Statement   2
       }

   Output :

              Enter Employee Id : 10
              Enter Employee Name : Ajay
              Enter Employee Age : 25
              Enter Employee Salary : 15000

              Employee Id : 10
              Employee Name : Ajay
              Employee Age : 25
              Employee Salary : 15000

				

In the above example, statement 1 is declaring Input() with return type Employee. As we know structure is user-defined data type and structure name acts as our new user-defined data type, therefore we use structure name as function return type.

Input() have local variable E of Employee type. After getting values from user statement 2 returns E to the calling function and display the values.

Related topics

Advertisement