How to pass a structure to a function

  • As we know,what are functions so while calling or declaring the function we have to pass a variable or a data.
  • As while passing variables while calling function or declaring function, data types plays an vital role
  • So now we will learn how to pass a structure to a Function,that is nothing but a variable which is made by us and we will pass it to a function
  • Let's first define a structure.
Let's define a structure Student with name ,rollno,div as it's attribute.

Definition:
struct student  //structure name
{
    int  rollno; //member1 ,integer variable which will store,
                 //student roll no.
                 
    char  name[20]; //member2 a character array of 20 character
                    //which will store name of student.
                    
    char  div;  //member3 a character variable,
              //which will store division of student.
};

Now we will define a function "struct_input" which will take a structure variable as as parameter and it will return nothing, Only it should print the members of structure variable

Prototype : declaration of function which takes structure as argument.
return_datatype  function_name(struct struct_name  struct_variable_name);

Defining a function:
void struct_input(struct student  s1)  //formal parameters
{ //this function will take data of student data type
    int student_rollno;  //variable
    char student_div;
    char student_name[20]; // character array


//Assigning the members of s1 to variable.

    student_rollno = s1.rollno;
    student_div   = s1.div;
//strcoy() is string handling function
//which assign the strings to an character array
    strcpy(student_name, s1.name);

//printing the values

 printf("student  roll no is %d ",student_rollno);
 printf("\nstudent name is %s",student_name);
 printf("  and studies in %c division",student_div);


}

Now let's call the function struct_input

Calling the function:
void main()
{
 int s_rollno;
 char s_name[20];
 char s_div;
 struct student s1={1,"Roy",'A'};
 //initialized  structure student.
//calling function struct_input()
//by passing variable s1 of 
//struct student datatype

struct_input(s1);

}

Output:

student roll no is 1.
student name is Roy and studies in A division

This is how we pass a structure variable to function.
Let's see an another example


Definition:
struct employee  //structure name
{
    int employee_id; //member1 int variable which,
                    //will store employee id

    char employee_name[20];//member2, character array which will,
                          //store name of employee.


    int employee_salary; //member4, int variable which will,
                        //store employee salary.


};

Now we will define a function "struct_input" which will take a structure variable as as parameter and it will return nothing, Only it should print the members of structure variable

Defining a function:
void struct_input(struct employee  E1)  //formal parameters
{ //this function will take data of employee data type
    int emp_id;  //variable
    char emp_name[20]; //character array
    int emp_salary;


//Assigning the members of E1 to variable.

    emp_id = E1.employee_id;
    emp_salary   = E1.employee_salary;
//strcoy() is string handling function
//which assign the strings to an character array
    strcpy(emp_name, E1.employee_name);

//printing the values

 printf("Employee id  is %d ",emp_id);
 printf("\nEmployee name is %s",emp_name);
 printf("  and has salary of %d $ ",emp_salary);


}

Now let's call the function struct_input

Calling the function:
void main()
{
 int emp_id;
 char emp_name[20];
 int emp_salary;
 struct employee E1={20,"Roy",500000};
 //initialized  structure employee.
//calling function struct_input()
//by passing variable E1 of
//struct employee  datatype

struct_input(E1);

}

Output:

Employee id is 20.
Employee name is Roy and has salary of 500000 $

This is how we pass a structure variable to function.so, do more practice to have a good command on structures







Further Concept:


People Also Searched: