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.
};
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);
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);
}
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);
}
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
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.
};
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);
}
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);
}
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 $
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:
- What are Nested Structure?
- How to built an Nested Structure?
- How to Access members of Nested Structure?
- How to initialize Nested structure?
- Array and Structure
- How to make array of structure variable?
- How to initialize array structure variable?
- How to Access members of an array of structure?
- How to take members of array structure as input from the user?
- Pointer and Structure
- How to Point a structure variable?
- How to declare a pointer of structure variable?
- How to Initialize an pointer of structure variable?
- How to access member of structure variable which is point by an pointer?
- What is '->' Operator?
- How to allocate memory for structure variable Dynamically
- How to allocate memory of structure variable using malloc() and calloc() function
- Self Referential Block
- What is Referential Structure?
- How to Create self Referential Structure?
- Applications of Self Referential Structures
- Practice Programs
People Also Searched:
- What are structures?
- Why structures?
- Need of Structure
- Concept of structure
- How to initialize an structure variable?
- How to Access Structure member?
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() function
0 Comments
Post a Comment