Chapter 8 Structure and Union



Structure :
Structures help to organize complex data in a more meaningful way. It is a powerful concept that we may often need to use in our program design.
A structure definition creates a format that may be used to declare structure variables. The general format of structure definition is as follows :
struct tag_name
{

data_type
member1;

data_type
member2;

....

....
};
Example :
#include<stdio.h>
struct  student
{
int roll_no;
char name[20];
int sub1;
int sub2;
int sub3;
int total;
float avg;
char grade;
};
main()
{
  struct student s;
  printf("\nINPUT ROLL NO   : ");
  scanf("%d",&s.roll_no);
  printf("INPUT NAME      : ");
  scanf("%s",s.name);
  printf("INPUT SUB1 MARK : ");
  scanf("%d",&s.sub1);
  printf("INPUT SUB2 MARK : ");
  scanf("%d",&s.sub2);
  printf("INPUT SUB3 MARK : ");
  scanf("%d",&s.sub3);
  s.total=s.sub1+s.sub2+s.sub3;
  printf("*****************************\n");
  printf("TOTAL   : %d\n",s.total);
  s.avg=float(s.total)/3;
  printf("AVERAGE : %f\n",s.avg);
  if (s.avg<35)
s.grade='D';
  else if(s.avg <50)
s.grade='C';
else   if(s.avg<60)
s.grade='B';
else
s.grade='A';
  printf("\nGRADE : %c",s.grade);

  printf("\naddress of roll_no is %u",&s.roll_no);
  printf("\naddress of name is %u",&s.name);
  printf("\naddress of sub1 is %u",&s.sub1);
  printf("\naddress of sub2 is %u",&s.sub2);
  printf("\naddress of sub3 is %u",&s.sub3);
  printf("\naddress of total is %u",&s.total);
  printf("\naddress of avg is %u",&s.avg);
  printf("\naddress of grade is %u",&s.grade);
}


Unions :
Unions are a concept borrowed from structures and there fore follow the same syntax as structures. However, there is major distinction between them in terms of storage. In structures each members has its own storage location, where all the members of a union use the same location.
format :
union tag_name
{

data_type
member1;

data_type
member2;

....

....
};
Example :
#include<stdio.h>
union  student
{
int roll_no;
char name[20];
int sub1;
int sub2;
int sub3;
int total;
float avg;
char grade;
};
main()
{
  union student s;
  printf("\nINPUT ROLL NO   : ");
  scanf("%d",&s.roll_no);
  printf("INPUT NAME      : ");
  scanf("%s",s.name);
  printf("INPUT SUB1 MARK : ");
  scanf("%d",&s.sub1);
  printf("INPUT SUB2 MARK : ");
  scanf("%d",&s.sub2);
  printf("INPUT SUB3 MARK : ");
  scanf("%d",&s.sub3);
  s.total=s.sub1+s.sub2+s.sub3;
  printf("*****************************\n");
  printf("TOTAL   : %d\n",s.total);
  s.avg=float(s.total)/3;
  printf("AVERAGE : %f\n",s.avg);
  if (s.avg<35)
s.grade='D';
  else if(s.avg <50)
s.grade='C';
else   if(s.avg<60)
s.grade='B';
else
s.grade='A';
  printf("\nGRADE : %c",s.grade);

  printf("\naddress of roll_no is %u",&s.roll_no);
  printf("\naddress of name is %u",&s.name);
  printf("\naddress of sub1 is %u",&s.sub1);
  printf("\naddress of sub2 is %u",&s.sub2);
  printf("\naddress of sub3 is %u",&s.sub3);
  printf("\naddress of total is %u",&s.total);
  printf("\naddress of avg is %u",&s.avg);
  printf("\naddress of grade is %u",&s.grade);
}