CHAPTER 10
“STRUCTURES AND UNIONS”
INTRODUCTION TO STRUCTURE
 Arrays are used to store large set of data and
  manipulate them but the disadvantage is that all
  the elements stored in an array are to be of the
  same data type.
 If we need to use a collection of different data type
  items it is not possible using an array.
 When we require using a collection of different
  data items of different data types we can use a
  structure.
 Structure is a method of packing data of
  different types.
 A structure is a convenient method of handling a
  group of related data items of different data types. 2
DEFINING STRUCTURE
   The general form for defining structure:
         struct tag_name
         {
         data type member1;
         data type member2;
         …
         …
         };
   Create a structure Book to store book details
   The keyword struct declares a structure to holds the details of
    four fields namely title, author, pages and price.
   These are members or elements of the structures. Each
    member may belong to different or same data type.
   The tag name can be used to define objects that have the tag
    names structure.
                                                                    3
   The structure we just declared is not a variable by itself but a
    template for the structure.
DEFINING STRUCTURE
 Example:
     struct lib_books
     {
     char title[20];
     char author[15];
     int pages;
     float price;
     };
                        4
DECLARING STRUCTURE VARIABLE
 To  access structure item, we require to create
  structure variable (object).
 A structure variable declaration is similar to
  the declaration of variables of any other data
  types.
 It includes the following elements:
       1.   The keyword struct.
       2.   The structure tag name.
       3.   List of variable names separated by names.
       4.   A terminating semicolon.
 Syntax:-                                               5
struct structure_name variable_name;
DECLARING STRUCTURE VARIABLE
 We    can declare structure variables using the
    tag name any where in the program.
   For example the statement,
        struct lib_books book1,book2,book3;
 declares   book1,book2,book3 as variables of type
    struct lib_books each declaration has four
    elements of the structure lib_books.
   Structures do not occupy any memory until it is
    associated with the structure variable such as book1.
                                                        6
DECLARING STRUCTURE VARIABLE
 We   can also combine both template declaration
    and variables declaration in one
   Statement, the declaration
    struct lib_books
    {
        char title[20];
        char author[15];
        int pages;
        float price;
    } book1,book2,book3;
 is valid.                                          7
DECLARING STRUCTURE VARIABLE
   The use of tag name is optional for example
 struct
  {
  …
  …
  …
  } book1, book2, book3 ;
   declares book1,book2,book3 as structure variables
    representing 3 books but does not include a tag name
    for use in the declaration.
                                                       8
ACCESSING STRUCTURE MEMBERS
   The link between a member and a variable is
    established using the member selection operator ‘.’
    which is known as dot operator or period
    operator.
 For    example:
    Book1.price
    is the variable representing the price of
    book1 and can be treated like any other
    ordinary variable.                      9
ACCESSING STRUCTURE MEMBERS
 We   can use scanf statement to assign values
 like
 scanf(“%s”,book1.title);
 scanf(“%d”,&book1.pages);
 or we can assign variables to the members of
 book1
 strcpy(book1.title,”basic”);
 strcpy(book1.author,”Balagurusamy”);
 book1.pages=250;
 book1.price=285.0;                               10
Write a C program that demonstrates the use of
structure.
#include<stdio.h>                            scanf("%s",s2.name);
#include<conio.h>                              printf("Enter the roll number of
struct student                               student2:");
{                                              scanf("%d",&s2.num);
   char name[20];                              printf("Enter the marks of student2:");
   int num;                                    scanf("%f",&s2.marks);
   float marks;                                printf("Detail of Students\n");
};
int main()                                   printf("______________________________
{                                            ___\n");
   struct student s1,s2;                       printf("%s\n",s1.name);
   printf("Enter the name of student1:");      printf("%d\n",s1.num);
   scanf("%s",s1.name);                        printf("%f\n",s1.marks);
   printf("Enter the roll number of
student1:");                                 printf("______________________________
   scanf("%d",&s1.num);                      ____\n");
   printf("Enter the marks of student1:");     printf("%s\n",s2.name);
   scanf("%f",&s1.marks);                      printf("%d\n",s2.num);
   printf("Enter the name of student2:");      printf("%f\n",s2.marks);
                                               return 0;                         11
                                             }
Write a program to create structure name student. Enter the
student’s name, Roll No, and marks of five subjects. Find out the
percentage and also define the grade of the student.
#include<stdio.h>                                if(s1.per>=90 && s1.per<=100)
#include<string.h>                                          strcpy(s1.grade,"A+");
                                                 else if(s1.per>79 && s1.per<90)
struct student                                              strcpy(s1.grade,"A");
{                                                else if(s1.per>69 && s1.per<80)
char name[50];                                              strcpy(s1.grade,"B+");
char rollno[50];                                 else if(s1.per>59 && s1.per<70)
int m1,m2,m3,m4,m5;                                         strcpy(s1.grade,"B");
float per;                                       else if(s1.per>49 && s1.per<60)
char grade[3];                                              strcpy(s1.grade,"C+");
};                                               else if(s1.per>39 && s1.per<50)
                                                            strcpy(s1.grade,"C");
int main()                                       else
{                                                           strcpy(s1.grade,"F");
struct student s1;
printf("Enter the name, rollno & marks of five   printf("\nName=%s\nRoll
subjects");                                      No=%s\nPercentage=%f\nGrade=
scanf("%s%s%d%d%d%d%d",s1.name,s1.rollno,&       %s",s1.name,s1.rollno,s1.per,s1.gra
s1.m1,&s1.m2,&s1.m3,&s1.m4,&s1.m5);              de);
s1.per =                                         printf("\nSize of struct is
(float)(s1.m1+s1.m2+s1.m3+s1.m4+s1.m5)/5;        %d",sizeof(s1));                 12
                                                 return 0;
                                                 }
COMPARING AND COPYING STRUCTURE VARIABLES
STRUCTURE INITIALIZATION
 Example,
 struct student
 {
  int weight;
  float height;
 } s3 = {70,181.57};
 struct student s1={61,161.51};
 struct student s2={55,150.25};
                                  14
ARRAYS OF STRUCTURES
 We may declare an array of structures, each
  element of the array representing a structure
  variable.
 For example,
      struct lib_books book[100];
 This declares the book as an array of 100
  elements book[0],book[1],book[2],…, and
  book[99].
 We can access structure members using array
  index like,
      book[0].price=100;    book[1].price = 150;
                                               15
Write a C program that demonstrates the use of arrays
of structure.
#include<stdio.h>         for(i=0; i<5; i++)
#include<conio.h>           {
struct student              printf("Enter the name, number
{                         and marks of student %d:",i);
   char name[20];            scanf("%s%d%f",s[i].name,
   int num;               &s[i].num,&s[i].marks);
   float marks;              }
};                        printf("Detail of Students\n");
int main()                  for(i=0;i<5;i++)
{                           {
   struct student s[5];   printf(“\nName:%s \nNumber:%d
   int i;                 \nMarks%f\n",s[i].name,
                          s[i].num,s[i].marks);
                             }                            16
                          return 0;
                          }
UNIONS
 Unions follows the same syntax as structures
 The major distinction between the two is in terms of
  storage
 In structures, each member has its own storage
  location whereas all members of a union use the same
  storage location.
 It implies that although a union may contain many
  members of different types, it can handle only
  member at a time.
 The size of a union is equal to the size of it's largest
  data member.
Fig. 10.7 Sharing of a storage locating by union members
Thank You