Thursday, February 23, 2012

Structures & Union.



Structures & Union


Structures are a form of storage like arrays. The difference between an array and a structure is in an array all components are of the same type where in a structure you can have different types of data. In general an object may need a variety of data types, for example to keep record of a student's marks we need - name, roll no, marks etc. To store these set of data under  a single variable name as Student and it is possible to access all these parameters under one name as student.  All these can be achieved by using a data type called a Structure.  A Structure is a group of one or more variables, usually of various types, and identified by a single name.

Declare a Structure

To declare a structure one has to first define the contents of the structure.  This will then be used as a template to declare variables of that particular structure. 

struct student
{  char name[20] ;
    int roll,eng,science,maths;
} stud;

Here, student is the name of the structure, also known as structure tag and stud is structure label or structure variable. A structure label can be created later by using structure name, like this struct student stud;

To access elements in a structure, a record selector (.) is use.

To declare additional variables of the same type, use of the keyword "struct" followed by the <struct type name>, followed by the variable names.

Structures can contain other structures as members, it is known as nested structure.
                  
Example :

 struct date_of_birth {
 int dd,mm,yy;
  };
 
 struct personal {
 char name[20],address[30];
 struct date_of_birth  dob;
  };

Arrays of structures

The arrays of basic types such as integers and floats are allowed in C, so are arrays of structures. An array of structures is declared in the usual way:

struct student
{  char name[20] ;
    int roll,eng,science,maths;
} stud[5];


Using typedef we can create an "alias" types from structure label(variable).

Example:
typedef struct
{  char name[20] ;
    int roll,eng,science,maths;
} student;

student stud;


Union

Unions are declared in the same fashion as structs, but have a fundamental difference. Only one item within the union can be used at any time, because the memory allocated for each item inside the union is in a shared memory location.

union student
{  char name[20] ;
    int roll,eng,science,maths;
} stud;


Different Between Array and Structure

Array

Structure
  1. In an array all components are of the same type.
  2. In an array the components are identified by a number as in table[37].
  3. An array we declare the array for each variable.

  1. In a structure you can have different types.
  2. A structure we refer to the individual components by name.
  3. In structure we declare a Structure first then use our structure declaration as a new type and declare variables of this type.



Different Between Structure and Union

Structure

Union
  1. Structure is a combination elements, which can be predefined data types or other structure. The length/size of the structure is the sum of the length of its elements.
  2. A structure allocates the memory equal to the total memory required by the members.
  3.  structure, each member have their own memory space.

  1. Union is a combination elements, which can be predefined data types or other union . But, the size/length of union is the maximum of internal elements.
  2. union allocates the memory equal to the maximum memory required by the member of the union. 
  3. In union, one block is used by all the member of the union.


Example of Union

#include <stdio.h>
#include <conio.h>

void main()
      {
           union books
              {
                   char bname[20],author[20];
                   int nc;
              }book;
           clrscr();
           printf("\nEnter Book Name... ");
           gets(book.bname);
           printf("\nName :  %s ",book.bname);
           printf("\nEnter Author Name... " );
           gets(book.author);
           printf("\nAuthor Name :\n",book.author);
           printf("\nEnter the no. copies...");
           scanf("%d",&book.nc);
           printf("\nNo. of Copies : %d\n",book.nc);
           getch();
      }

Example of Simple Structure

#include <stdio.h>
#include <conio.h>

void main()
      {
           struct books
              {
                   char bname[20],author[20];
                   int nc;
              }book;
           clrscr();
           printf("\nEnter Book Name... ");
           gets(book.bname);
           printf("\nEnter Author Name... " );
           gets(book.author);
           printf("\nEnter the no. copies...");
           scanf("%d",&book.nc);
           printf("\nAuthor Name :\n",book.author);
           printf("\nName :  %s ",book.bname);
           printf("\nNo. of Copies : %d\n",book.nc);
           getch();
      }

Example of Array / Nested Structure

#include <stdio.h>
#include <conio.h>

void main()
      {
           struct date
              {
                   int dd,mm,yy;
              };
           struct emp
              {
                   int ecode;
                   char name[20];
                   struct date jd;
                   float sal;
              }em[10];
           int i,n;
           float s;
           clrscr();
           printf("\n\tEnter Nos. of Employee [below 10] : ");
           scanf("%d",&n);
           for(i=0;i<n;i++)
              {
                   printf("\n\tEnter Employee Code : ");
                   scanf("%d",&em[i].ecode);
                   printf("\n\tEnter Employee Name : ");
                   fflush(stdin);
                   gets(em[i].name);
                   fflush(stdin);
                   printf("\n\tEnter Employee Date of Birth : ");
                   printf("\n\t\tEnter Day : ");
                   scanf("%d",&em[i].jd.dd);
                   printf("\n\t\tEnter Month : ");
                   scanf("%d",&em[i].jd.mm);
                   printf("\n\t\tEnter Year : ");
                   scanf("%d",&em[i].jd.yy);
                   printf("\n\tEnter Employee Salary : ");
                   scanf("%f",&s);
                   em[i].sal=s;
              }
           printf("\n\n\t The data \n");
           printf("\n------------------------------------------------------------");
           printf("\n  Code    Name                   Date of Join       Salary");
           printf("\n------------------------------------------------------------");
           for(i=0;i<n;i++)
              {
                   printf("\n  %d\t  %-20s\t%2d-%2d-%4d\t%8.2f",em[i].ecode,em[i].name,em[i].jd.dd,em[i].jd.mm,em[i].jd.yy,em[i].sal);
              }
           printf("\n------------------------------------------------------------");
           getch();
      }

Sorting with Array Structure

#include <stdio.h>
#include <conio.h>

  struct report
       {
           char name[20];
           int marks;
      }    rep[3],temp;

  void disp()
      {
           int i;
           for(i=0;i<3;i++)
              {
                   printf("\nName :  %s   Marks  : %d",rep[i].name,rep[i].marks);
              }
      }

  void main()
      {
           int i,j;
           clrscr();
           for(i=0;i<3;i++)
              {
                   printf("\nEnter Name : ");
                   fflush(stdin);
                   gets(rep[i].name);
                   printf("\nEnter Total Marks : ");
                   scanf("%d",&rep[i].marks);
               }
           printf("\nOriginal \n");
           disp();
           for(i=0;i<3;i++)
              {
                   for(j=0;j<3;j++)
                        {
                            if(rep[i].marks>rep[j].marks)
                                {
                                      temp=rep[i];
                                      rep[i]=rep[j];
                                      rep[j]=temp;
                                }
                        }
              }
           printf("\n\nSorted in Dscending Order\n");
           disp();
           getch();
      }


No comments: