STRUCTURE and UNION


Contents :

  1. Structure
  2. Array in Structures
  3. Structure with Array
  4. Structures within Structures (Nested Structures)

Structure :

Structure is user defined data type which is used to store heterogeneous data under unique name. Keyword 'struct' is used to declare structure.
The variables which are declared inside the structure are called as 'members of structure'.
Syntax:

struct structure_nm
{
 <data-type> element 1;
 <data-type> element 2;
 - - - - - - - - - - -
 - - - - - - - - - - -
 <data-type> element n;
}struct_var;


Example :

struct emp_info
{
 char emp_id[10];
 char nm[100];
 float sal;
}emp;

Note :
1. Structure is always terminated with semicolon (;).
2. Structure name as emp_info can be later used to declare structure variables of its type in a program.

* INSTANCES OF STRUCTURE :

Instances of structure can be created in two ways as,
Instance 1:

struct emp_info
{
 char emp_id[10];
 char nm[100];
 float sal;
}emp;


Instance 2:

struct emp_info
{
 char emp_id[10];
 char nm[100];
 float sal;
};
struct emp_info emp;
In above example, emp_info is a simple structure which consists of stucture members as Employee ID(emp_id), Employee Name(nm), Employee Salary(sal).

* ACEESSING STRUCTURE MEMBERS :

Structure members can be accessed using member operator '.' . It is also called as 'dot operator' or 'period operator'.
structure_var.member;

Program :


/*  Program to demonstrate structure.

Creation Date : 6 MAR 2012 02:41:01 AM

Author : HARISH */

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

struct comp_info
{
 char nm[100];
 char addr[100];
}info;

void main()
{
 clrscr();
 printf("\n Enter Company Name : ");
 gets(info.nm);
 printf("\n Enter Address : ");
 gets(info.addr);
 printf("\n\n Company Name : %s",info.nm);
 printf("\n\n Address : %s",info.addr);
 getch();
}

Output :


 Enter Company Name : HARISH WORLD, HARISH WEB SOLUTIONS
 Enter Address : xxxxxxxxxx, xxxxxxxxxx,AP, INDIA

 Company Name : HARISH WORLD,HARISH WEB SOLUTIONS
 Address : xxxxxxxxxx,xxxxxxxxxx,AP,INDIA



Array in Structures :

Sometimes, it is necessary to use structure members with array.

Program :


/*  Program to demonstrate array in structures.

Creation Date : 30 MAR 2012 06:07:11 PM

Author : HARISH */

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

struct result
{
 int rno, mrks[5];
 char nm;
}res;

void main()
{
 int i,total;
 clrscr();
 total = 0;
 printf("\n\t Enter Roll Number : ");
 scanf("%d",&res.rno);
 printf("\n\t Enter Marks of 3 Subjects : ");
 for(i=0;i<3;i++)
 {
  scanf("%d",&res.mrks[i]);
  total = total + res.mrks[i];
 }
 printf("\n\n\t Roll Number : %d",res.rno);
 printf("\n\n\t Marks are :");
 for(i=0;i<3;i++)
 {
  printf(" %d",res.mrks[i]);
 }
 printf("\n\n\t Total is : %d",total);
 getch();
}

Output :


 Enter Roll Number : 1
 
 Enter Marks of 3 Subjects : 63 66 68
 
 
 Roll Number : 1
 
 Marks are : 63 66 68
 
 Total is : 197_


Structures within Structures (Nested Structures) :

Structures can be used as structures within structures. It is also called as 'nesting of structures'.
Syntax:

struct structure_nm
{
 <data-type> element 1;
 <data-type> element 2;
 - - - - - - - - - - -
 - - - - - - - - - - -
 <data-type> element n;
 
 struct structure_nm
 {
  <data-type> element 1;
  <data-type> element 2;
  - - - - - - - - - - -
  - - - - - - - - - - - 
  <data-type> element n;
 }inner_struct_var;
}outer_struct_var;


Example :

struct stud_Res
{
 int rno;
 char nm[50];
 char std[10];
 
 struct stud_subj
 {
  char subjnm[30];
  int marks;
 }subj;
}result;
In above example, the structure stud_Res consists of stud_subj which itself is a structure with two members. Structure stud_Res is called as 'outer structure' while stud_subj is called as 'inner structure.' The members which are inside the inner structure can be accessed as follow :
result.subj.subjnm
result.subj.marks

Program :


/*  Program to demonstrate nested structures.

Creation Date : 30 MAR 2012 04:04:01 AM

Author : HARI */

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

struct stud_Res
{
 int rno;
 char std[10];
 struct stud_Marks
 {
  char subj_nm[30];
  int subj_mark;
 }marks;
}result;

void main()
{
 clrscr();
 printf("\n\t Enter Roll Number : ");
 scanf("%d",&result.rno);
 printf("\n\t Enter Standard : ");
 scanf("%s",result.std);
 printf("\n\t Enter Subject Code : ");
 scanf("%s",result.marks.subj_nm);
 printf("\n\t Enter Marks : ");
 scanf("%d",&result.marks.subj_mark);
 printf("\n\n\t Roll Number : %d",result.rno);
 printf("\n\n\t Standard : %s",result.std);
 printf("\nSubject Code : %s",result.marks.subj_nm);
 printf("\n\n\t Marks : %d",result.marks.subj_mark);
 getch();
}

Output :


 Enter Roll Number : 1
 
 Enter Standard : MCA(Sci)-I
 
 Enter Subject Code : SUB001
 
 Enter Marks : 63
 
 
 Roll Number : 1
 
 Standard : MCA(Sci)-I
Subject Code : SUB001

 Marks : 63_




No comments: