Structures and Functions

Structures may be passed to functions either by value or by reference. However, they are usually passed by reference. i.e.. the address of the structure is passed to the function. Let us consider the employee example for instance. Given the structure declaration below

struct employee
{
    char *name;
    float basic;
    float HRA;
}emp1;

assume that the gross salary has to be calculated by adding basic with HRA. The following function takes the address of the structure as its parameter, adds up the (wo (basic and HRA) and sends the gross salary as output.

Example 6.1

The function reads input from the user, initializes each of the members and returns the entire structure.

struct employee init()

/* return a value of type struct employee */
{
    struct employee emp;
    printf("
 Enter the employee 's name ");
    scanf ("%s", emp.name);
    fflush (stdin);
    printf ("
 Enter the employee's basic");
    scanf("%f", &emp.basic);
    fflush (stdin);
    return emp;
}

Unions

Consider a situation. Brainstorm is a computer institute offering many courses to students. It offers two major courses, and a lot of minor ones. The major courses are classified as majors - 1 and 2, and the minors are classified into 'others'. When a student enrolls for a particular course, his details are entered into a file and maintained till he completes the course. The student structure looks like this

Struct student
 {
    char *name;
    int rollno;
  };

Apart from this, in the students' register, one more detail has to be included - the course the particular student is enrolled for. But this poses a problem, since the two major courses are represented by numbers (1 and 2) and the minor courses by a string. The solution to this problem is -unions.

A union is a variable which may hold (at different times) objects of different types and sizes, with the compiler keeping track of size and alignment requirements. A union declaration is similar to a structure declaration

union course
{
    int major;
    char *minor;
 } course1;

Here, course1 will be large enough to hold the largest of the two types, int or char. Any one of these variables might be stored in course1 and then used in expressions. Only condition char *minor; ) course1; is that the usage must be consistent, the type retrieved must be the type stored last.

struct student
 {
    char *name;
    int rollno;
    union course course_no;
} student;

This would represent no storage problems as the member course _no has been declared as a union. Course_no can now contain any value - char or int. The following program illustrates the usage of unions

Example 6.2

#include <stdio.h>
#include <string.h>
union course
{
    int major;
    char minor [10];
};

struct student 
{
    char name [20];
    int rollno;
    union course course_no;
} student1;

void main ()
{
   char c_name;
   printf ("\nEnter the name of the student");
   scanf ("%s", student1.name);
   fflush (stdin);
   printf ("\aEnter the roll no. of the student");
   scanf ("%d", &student1.rollno);
   fflush (stdin);
   printf ("\nEnter the course ('M' for major or 'm' for minor)");
   scanf ("%c", &c_name);
   fflush (stdin);
   if (c_name == 'M')
   {
      printf ("\nEnter the course ('1' or '2') "); 
      scanf ("%d", &student1.course_no. major);
      fflush(stdin);
   }
   else
     strcpy (student1. course_no.minor, "others");
}