Beginners Guide to C Structures: Definition, creation and manipulation

We have learned to create group elements of the same type into a single logical entity array. If it looks like jargon, consider this, the variables are being put in a group, they are referred by a single name – and importantly they were all of the same types. Either all were integers, all floats, etc. what if we want to group different types of items into one logical entity say day, month, and year to be called the date? Further what if we want to group these elements into bigger entities?

Consider the following example: every date is made up of 3 parts day, month, and year. Day is an integer, month is a string of characters,s and year is an integer. Now suppose, we want to declare date of birth, date of joining service, date of marriage, etc. Each of them are similar to the variable date – each having a day which is an integer, a month which is a string of character and year, an integer.

C provides a nice way of declaring and managing such situations. It makes use of a concept called structure.

Struct date
 {
    int day;
    char month[3];
    int year;
 };

This means the date is a structure – it has 3 parts – an integer called day, a character string of 3 elements called month and another integer called year.

Now we can declare date of birth as a structure of the type date structure date date_of_birth. Similarly structure date date_of_mar etc..

To get the actual month, say, of date of marriage, We say date_of_marriage.date. To get the actual year of date of birth, We use date_of_birth.year and so on. We can use these as if they are variable names. A few examples will clarify their usage.

Program to illustrate a structure

main()
 {
    struct date
   {
      int month;
      int day;
      int year;
   };
   struct date today;
   today.month =9;
   today.day = 25;
   today.year = 1988;
   printf ("Today's date is %d/%d/%d\n", today.month,today.day,today.year %100);
 }

OUTPUT:

Today's date is 9/25/88.

You can use these structures in arrays also. Let us say you have an array class with 20 elements each elements pertaining to a student of that class. Now, extending the previous example, I can store the dates of birth of these 20 students, by saying

struct date birthdays[20]

Where there are 20 birthdays, each of the type date (day, month, year). Suppose I want to find the month of birth of 10th student, I can ask Birthdays[9].month

Birthdays is the array, 9 indicates it is the 10th student(note that indices start from 0)and, month indicates that we are referring to the month.

In fact, the whole situation can be shown as in figure below:

Now suppose we want to store the number of days in each month of the year and take a printout. The program looks something like this.

Program to illustrate structures and arrays

struct month
{ 
   int number_of_days;
   char name[3]]
};
main()
{
   int i;
   static struct month monts[12] =
   { {31,{'J', 'a', 'n'}}, {28, {'F', 'e', 'b' } },
     {31,{'M', 'a', 'r'}}, {30, {'A', 'p', 'r' } },
     {31,{'M', 'a', 'y'}}, {30, {'J', 'u', 'n' } },
     {31,{'J', 'u', 'l'}}, {31, {'A', 'u', 'g' } },
     {30,{'S', 'e', 'p'}}, {31, {'O', 'c', 't' } },
     {30,{'N', 'o', 'v'}}, {31, {'D', 'e', 'c' } } };
   printf ("Month Number of Days\n");
   printf ("_ _ _ _ _ _ _ _ _ _ _ _ _ _");
   for (i=0; i<12; ++i)
        printf ("%c%c%c %d\n", 
           months[i].name[0],months[i].name[1],
           months[i].name[2],months[i].number_of_days);
 }

OUTPUT:

Month        Number of Days
- - - - - - - - - - - - - - - - -
Jan            31
Feb            28
Mar            31
Apr            30
May            31
Jun            30
Jul            31
Aug            31
Sep            30
Oct            31
Nov            30
Dec            31 

In fact, there is some flexibility allowed in the declaration of structure type. Instead of first declaring the structure and then indicating the element name, they can be included in one statement.

For example:

Struct date
  {
     int day;
     char month[3];
     int year;
     date_of_birth, date_of_marriage;
  }

of course, it is a matter of individual preference and convenience to choose from amongst the options.

Related Post