• C Programming Video Tutorials

C - Structures and Functions



In C, 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 as well as call by reference methods. A function in C may also return a struct data type.

In this chapter, we shall learn to −

  • Pass elements of struct type
  • Pass struct variable
  • Return struct from function
  • Return struct pointer

Pass struct elements

A derived type is a combination of one or more elements of any of the primary types as well as the same or different derived type. It is possible to pass elements to a function, either by value or by reference.

In the following example, a derived type called rectangle with two elements. A struct variable r with has the elements r.len and l.brd, and they are passed to a function. The area() function then computes the area of rectangle.

Example

#include <stdio.h>
#include <string.h>
struct rectangle{
   float len, brd;
};
int area(float, float);
int main () {
   struct rectangle r;
   printf("Input length and breadth of a rectangle\n");
   //scanf("%f %f", &r.len, &r.brd);
   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 \n Breadth: %f \n Area: %lf\n", a, b, area);
   return 0;
}

Output

Input length and breadth of a rectangle
Length: 10.500000 
Breadth: 20.500000 
Area: 215.250000

Pass 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. Inside the function, the elements of the struct variable are accessed though the dot operator, and the area is calculated.

Example

#include <stdio.h>
#include <string.h>
struct rectangle{
   float len, brd;
   double area;
};
int area(struct rectangle);
int main () {
   struct rectangle r;
   printf("Input length and breadth of a rectangle\n");
   //scanf("%f %f", &r.len, &r.brd);
   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 \n Breadth: %f \n Area: %lf\n", r.len, r.brd, r.area);
   return 0;
}

Output

Input length and breadth of a rectangle
Length: 10.500000 
Breadth: 20.500000 
Area: 215.250000

Return struct from function

We know that a function in C is able to return a value of any of the types. In this example, the area() function is defined to return a struct variable.

In main(), inputs are accepted from the user for length and breadth, and are passed to the function. Inside the function, the area is computed, and a struct variable is populated and returned to the main() function, where its elements are displayed.

Example

#include <stdio.h>
#include <string.h>
struct rectangle{
   float len, brd;
   double area;
};
struct rectangle area(float x, float y);
int main () {
   struct rectangle r;
   float x, y;
   printf("Input length and breadth of a rectangle\n");
   //scanf("%f %f", &x, &y);
   x=10.5; y=20.5;
   r=area(x, y);
   printf("Length: %f \n Breadth: %f \n Area: %lf\n", 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

Input length and breadth of a rectangle
Length: 10.500000  
Breadth: 20.500000 
Area: 215.250000

Pass struct by reference

In C, a function may be defined to have its arguments passed by value or reference. A reference is the pointer to an existing variable. In the example explained below, a struct variable of rectangle type is declared in main() and its address is passed to a user−defined function − area(). When called, the area() function is able to use the elements of the variable with the indirection operator −>. It computes the result and assigns it to the area element r−>area.

Example

#include <stdio.h>
#include <string.h>
struct rectangle{
   float len, brd;
   double area;
};
int area(struct rectangle *);
int main () {
   struct rectangle r;
   printf("Input length and breadth of a rectangle\n");
   //scanf("%f %f", &r.len, &r.brd);
   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 \n Breadth: %f \n Area: %lf\n", r->len, r->brd, r->area);
   return 0;
}

Output

Input length and breadth of a rectangle
Length: 10.500000 
Breadth: 20.500000 
Area: 215.250000

Return struct pointer

Let us rewrite the above code to define the area() function and return a pointer to a struct rectangle data type. 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 main().

Example

#include <stdio.h>
#include <string.h>
struct rectangle{
   float len, brd;
   double area;
};
struct rectangle * area(float x, float y);
int main (){
   struct rectangle *r;
   float x, y;
   printf("Input length and breadth of a rectangle\n");
   //scanf("%f %f", &x, &y);
   x=10.5; y=20.5;
   r=area(x, y);
   printf("Length: %f \n Breadth: %f \n Area: %lf\n", 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

Input length and breadth of a rectangle
Length: 10.500000 
Breadth: 20.500000 
Area: 215.250000
Advertisements