Structures and functions can be used together to make a program more organized and reusable. A structure variable can be passed to a function so that the function can access or process the related data stored in the structure.
Structures and functions can be used together to make a program more organized and reusable. A structure variable can be passed to a function so that the function can access or process the related data stored in the structure.
A Student structure can be passed to a function
named displayStudent() to display the student's
details.
A structure variable can be passed to a function just like other variables. The called function receives a copy of the structure and can use its members.
Here, s1 is a structure variable passed to the
function displayStudent().
The parameter of a function can be declared using the structure type. This allows the function to receive a complete structure record.
The function receives a struct Student variable
as its parameter and accesses its member using the dot operator.
Once a structure variable is available inside a function, its
members can be accessed using the dot operator in the same way
as in main().
These expressions access individual members of the structure
variable s.
Combining structures and functions helps divide a program into smaller tasks. One function can accept a record, process it, or display it, while other parts of the program remain unchanged.
In a student management program, one function can read student details and another function can display those details.
Write a C program to create a student structure and pass a structure variable to a function that displays the student's roll number and percentage.
Define a Student structure, create a structure variable,
and pass it to a function named displayStudent().
A structure can be passed to a function as an argument. When a structure is passed by value, the function receives a copy of the structure and can access its members using the dot operator.
Enter roll number: 101
Enter percentage: 82.5
Roll Number = 101
Percentage = 82.5
In this program, s1 is passed to
displayStudent(). The function receives the structure
and accesses its members using s.rollNo and
s.percentage.
A function can also return a complete structure to the calling function. The return type of the function is declared as the corresponding structure type.
The function creates a Student structure and returns
it to the calling function.
| Concept | Remember |
|---|---|
| Structure as Argument | A structure variable can be passed to a function. |
| Structure Parameter | Function parameter can be declared as a structure type. |
| Member Access | Use the dot operator inside the function. |
| Return Structure | A function can return a complete structure. |
Watch a beginner-friendly explanation of passing structures to functions in C.
A short handwritten-style revision sheet for structures and functions will be provided here.
Use the mind map for quick revision of structure arguments, parameters, member access and return values.