C program to pass an array of structure to a user-defined function
Given an array of structure and we have to pass it to the function in C.
structure declaration is:
struct exam
{
int roll;
int marks;
char name[20];
};
Here,
- exam is the structure name
- roll, marks and name are the members of the structure/variable of the structure
Here is the function that we are using in the program,
void structfun(struct exam obj1);
Here,
- void is the returns type of the function i.e. function will not return any value.
- structfun is the name of the function.
- struct exam obj1 is the object of exam structure - while declaring a function we must have to use struct keyword with the structure name.
Structure object/variable declaration is:
struct exam obj[2];
obj is an array of structure for 2 structures.
Assigning values to the structure variables (array of structure variables) inside the main()
// assign values using the object 1
obj[0].marks = 35;
obj[0].roll = 10;
strcpy(obj[0].name , "Arjun kapoor");
// assign values using the object 1
obj[1].marks = 75;
obj[1].roll = 11;
strcpy(obj[1].name , "Balram chauhan");
Function calling statement,
structfun(obj);
Program to pass a structure to a function in C
Output
need an explanation for this answer? contact us directly to get an explanation for this answer