”;
In C programming, struct is a derived data type. Just as we can pass arguments of primary data types, a variable of struct data type can also be passed to a function. You can also pass structures using call by value and call by reference methods. A function in C may also return a struct data type.
Read this chapter to understand the following concepts −
- How to pass elements of struct type
- How to pass a struct variable
- How to return struct from a function
- How to return a struct pointer
Let”s start with the first one and learn how to pass elements of struct type.
How to Pass Struct Elements
A derived type is a combination of one or more elements of any of the primary types as well as another derived type. It is possible to pass elements to a function, either by value or by reference.
Example
In the following example, we have a derived type called “rectangle” with two elements. We have a struct variable “r” with the elements “r.len” and “l.brd” and they are passed to a function. The area() function then computes the area of the rectangle.
#include <stdio.h> struct rectangle{ float len, brd; }; int area(float, float); int main(){ struct rectangle r; r.len = 10.50; r.brd = 20.5; area(r.len, r.brd); return 0; } int area(float a, float b){ double area = (double)(a*b); printf("Length: %f nBreadth: %f nArea: %lfn", a, b, area); return 0; }
Output
When you run this code, it will produce the following output −
Length: 10.500000 Breadth: 20.500000 Area: 215.250000
How to Pass a Struct Variable
Let us modify the above example to pass the struct variable itself (instead of its elements) to the area() function. The rectangle struct type also has an additional element called “area”.
Example
Inside the function, the elements of the struct variable are accessed though the dot operator (.) and the area is calculated.
#include <stdio.h> struct rectangle{ float len, brd; double area; }; int area(struct rectangle); int main(){ struct rectangle r; r.len = 10.50; r.brd = 20.5; area(r); return 0; } int area(struct rectangle r){ r.area = (double)(r.len*r.brd); printf("Length: %f nBreadth: %f nArea: %lfn", r.len, r.brd, r.area); return 0; }
Output
Run the code and check its output −
Length: 10.500000 Breadth: 20.500000 Area: 215.250000
How to Return Struct from a Function
We know that a function in C can return a value of any type. In this example, the area() function is defined to return a struct variable.
Example
Inside the main() fuction, the inputs for the length and the breadth are passed to the area() function. Inside the area() function, the area is computed and a struct variable is populated and returned to the main() function, where its elements are displayed.
#include <stdio.h> struct rectangle { float len, brd; double area; }; struct rectangle area(float x, float y); int main(){ struct rectangle r; float x, y; x = 10.5; y = 20.5; r = area(x, y); printf("Length: %f n Breadth: %f n Area: %lfn", r.len, r.brd, r.area); return 0; } struct rectangle area(float x, float y){ double area = (double)(x*y); struct rectangle r = {x, y, area}; return r; }
Output
When you run this code, it will produce the following output −
Length: 10.500000 Breadth: 20.500000 Area: 215.250000
How to Pass a Struct by Reference
In C language, a function may be defined to have its arguments passed by value or reference. A reference is the pointer to an existing variable.
Example
In this example, a struct variable of “rectangle” type is declared in main() and its address is passed to a user-defined function called area().
When the area() function is called, it can use the elements of the variable with the indirection operator (→). It computes the result and assigns it to the area element “r → area”.
#include <stdio.h> struct rectangle{ float len, brd; double area; }; int area(struct rectangle *); int main(){ struct rectangle r; r.len = 10.50; r.brd = 20.5; area(&r); return 0; } int area(struct rectangle *r){ r -> area = (double)(r -> len * r -> brd); printf("Length: %f nBreadth: %f nArea: %lfn", r -> len, r -> brd, r -> area); return 0; }
Output
Run the code and check its output −
Length: 10.500000 Breadth: 20.500000 Area: 215.250000
How to Return a Struct Pointer
Let us rewrite the above code to define the area() function and return a pointer to a struct of rectangle data type.
Example
The area() function has two call-by-value arguments. The main() function reads the length and breadth from the user and passes them to the area() function, which populates a struct variable and passes its reference back to the main() function.
#include <stdio.h> struct rectangle { float len, brd; double area; }; struct rectangle * area(float x, float y); int main (){ struct rectangle *r; float x, y; x = 10.5; y = 20.5; r = area(x, y); printf("Length: %f n Breadth: %f n Area: %lfn", r->len, r->brd, r->area); return 0; } struct rectangle * area(float x, float y){ double area = (double)(x*y); static struct rectangle r; r.len = x; r.brd = y; r.area = area; return &r; }
Output
When you run this code, it will produce the following output −
Length: 10.500000 Breadth: 20.500000 Area: 215.250000
”;