User-Defined Datatypes

C allows programmers to define new datatypes equivalent to the existing system datatypes using the typedef statement. Let us take the employee structure for example. Assume that the employee's date of birth has to be included in the existing structure. The declaration would be

typedef struct
 {
     int dd; /* date */
     int mm; /* month */
     int YY; /* year */
} dob;

Given this declaration, dob can be used in the structure just as if it were another datatype. like int, char and so on. For example.

dob emp_date_of_birth;

 Pointers to Structures

Pointers to structures are just like pointers to other ordinary variables. The declaration

struct employee {

…

…

…

} *emp1;

declares a pointer of type struct employee. If emp1 points to a structure, then *emp1 is the pointer to the structure, and either (*emp1) .name or emp1- > name might be used to access the structure members. Here. - > is the member access operator for pointers, which is nothing but a hyphen '-' followed by the greater than symbol '>'.

The arithmetic for a pointer of this type is same as for any other datatype, and any manipulation that can be done through pointers to variables of other types is also possible for structures.

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

#include <stdio.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 ("
Enter 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 ("
Enter the course ('M' for major or 'm" for minor)");
    scanf ("%c". &C_name);
    fflush (stdin);
    if (c_name == 'M')
    {
        printf ("
Enter the course ('1' or '2") "); 
        scanf ("%d", &student1.course_no. major);
        fflush(stdin);
     }
     else
        strcpy (student1. course_no.minor, "others");
}