Structures and Unions
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
19
1
Outline
 What is a Structure?
 Defining a Structure
 General format of a Structure
 Arrays vs. Structure
 Accessing Structure Members
 Rules for initializing Structures
 What is a Union?
 Difference between Structure and Union
What is a Structure?
 We can not use an array if we want to represent a
collection of data items of different types using a
single name.
 Structures are a mechanism for packing data of
different types.
 A structure is a convenient tool for handling a group
of logically related data items.
What is a Structures? (cont..)
 Examples of structures:
time : seconds, minutes, hours
date : day, month, year
book : author, title, price, year
city : name, country, population
 Structures help to organize complex data in a more
meaningful way
Defining a Structure
 Consider a book database consisting of book name, author,
number of pages, and price.The structure to hold this
information as follows:
struct book_name
{
char title[20];
char author[15];
int pages ;
float price;
}
Defining a Structure (cont..)
 The keyword struct declares a structure to hold the
details of four data fields, namely title, author,
pages, and price.These fields are called structure
elements or members.
 Each member may belong to a different type of data.
 book_name is the name of the structure and is
called the structure tag.
Template of a Structure (cont..)
General format of a Structure
Arrays vs. Structure
1. An array is a collection of related data elements of
same types. Structure can have elements of different
types.
2. An array is derived data type whereas a structure is
a programmer-defined one.
3. Any array behaves like a built-in data type.All we
have to do is to declare an array variable and use it.
But in the case of a structure, first we have to
design and declare a data structure before the
variables of that type are declared and used.
Declaring a Structure
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
} book1, book2, book3;
 Here book1, book2, book3 are declared as variables
of type struct book_bank .
Accessing Structure Members
 The link between a member and a variable is
established using the member operation ('.’)
 From the previous example:
Book1.price
 It is a variable representing the price of book1.
 We can use scanf to give the value through keyboard
scanf(“%s”, book1.title);
scanf(“%d”, &book1.pages);
Accessing Structure Members (Code)
struct personal
{
char name[20];
int day;
char month[10];
int year;
float salary;
};
int main()
{
struct personal person;
printf(“InputValue: ”);
Accessing Structure Members (Code)
scanf(“%s %d %s %d %f”,
person.name, &person.day, person.month
&person.year, &person.salary);
printf(“Output: %s %d %s %d %fn”,
person.name, person.day, person.month
person.year, person.salary);
}
Output:
Input values: Mackangee 10 june 1945 4500
Output: Mackangee 10 june 1945 4500.00
Rules for initializing Structures
1. We cannot initialize individual members inside the structure
template.
2. The order of values enclosed in braces must match the order of
members in the structure definition.
3. It is permitted to have a partial initialization.We can initialize only
the first few members and leave the remaining blank.The
uninitialized members should be only at the end of the list.
4. The uninitialized members will be assigned default values as
follows:
 Zero for integer and floating point numbers.
 ‘0’ for characters and strings.
What is a Union?
 Unions are a concept borrowed from structures and
therefore follow the same syntax as structures.
 The major distinction between structure and union is
the storage.
 In structure, each member has its own storage
location, whereas all the members of a union use the
same location.
 Although a union may contain many members o
different types, it can handle only one member at a
time.
Unions (Example1)
 For example in the following C program, both x and y share the same location. If we change x, we can
see the changes being reflected in y.
– union test {
– int x, y;
– };
– int main()
– {
– union test t;
– t.x = 2; // t.y also gets value 2
– printf("After making x = 2:n x = %d, y = %dnn", t.x, t.y);
– t.y = 10; // t.x is also updated to 10
– printf("After making y = 10:n x = %d, y = %dnn", t.x, t.y);
– return 0;
– }
Unions (Example1) (cont..)
 Output:
– After making x = 2:
– x = 2, y = 2
– After making y = 10:
– x = 10, y = 10
Unions (Example2)
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
Unions (Example2) (cont…)
int main( ) {
union Data data;
data.i = 10;
printf( "data.i : %dn", data.i);
data.f = 220.5;
printf( "data.f : %fn", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %sn", data.str);
return 0;
}
Unions (Example2) (cont…)
Output:
data.i : 10
data.f : 220.500000
data.str : C Programming
Difference between Structure and Union
Lecture 19 - Struct and Union

Lecture 19 - Struct and Union

  • 1.
    Structures and Unions Md.Imran Hossain Showrov (showrovsworld@gmail.com) 19 1
  • 2.
    Outline  What isa Structure?  Defining a Structure  General format of a Structure  Arrays vs. Structure  Accessing Structure Members  Rules for initializing Structures  What is a Union?  Difference between Structure and Union
  • 3.
    What is aStructure?  We can not use an array if we want to represent a collection of data items of different types using a single name.  Structures are a mechanism for packing data of different types.  A structure is a convenient tool for handling a group of logically related data items.
  • 4.
    What is aStructures? (cont..)  Examples of structures: time : seconds, minutes, hours date : day, month, year book : author, title, price, year city : name, country, population  Structures help to organize complex data in a more meaningful way
  • 5.
    Defining a Structure Consider a book database consisting of book name, author, number of pages, and price.The structure to hold this information as follows: struct book_name { char title[20]; char author[15]; int pages ; float price; }
  • 6.
    Defining a Structure(cont..)  The keyword struct declares a structure to hold the details of four data fields, namely title, author, pages, and price.These fields are called structure elements or members.  Each member may belong to a different type of data.  book_name is the name of the structure and is called the structure tag.
  • 7.
    Template of aStructure (cont..)
  • 8.
    General format ofa Structure
  • 9.
    Arrays vs. Structure 1.An array is a collection of related data elements of same types. Structure can have elements of different types. 2. An array is derived data type whereas a structure is a programmer-defined one. 3. Any array behaves like a built-in data type.All we have to do is to declare an array variable and use it. But in the case of a structure, first we have to design and declare a data structure before the variables of that type are declared and used.
  • 10.
    Declaring a Structure structbook_bank { char title[20]; char author[15]; int pages; float price; } book1, book2, book3;  Here book1, book2, book3 are declared as variables of type struct book_bank .
  • 11.
    Accessing Structure Members The link between a member and a variable is established using the member operation ('.’)  From the previous example: Book1.price  It is a variable representing the price of book1.  We can use scanf to give the value through keyboard scanf(“%s”, book1.title); scanf(“%d”, &book1.pages);
  • 12.
    Accessing Structure Members(Code) struct personal { char name[20]; int day; char month[10]; int year; float salary; }; int main() { struct personal person; printf(“InputValue: ”);
  • 13.
    Accessing Structure Members(Code) scanf(“%s %d %s %d %f”, person.name, &person.day, person.month &person.year, &person.salary); printf(“Output: %s %d %s %d %fn”, person.name, person.day, person.month person.year, person.salary); } Output: Input values: Mackangee 10 june 1945 4500 Output: Mackangee 10 june 1945 4500.00
  • 14.
    Rules for initializingStructures 1. We cannot initialize individual members inside the structure template. 2. The order of values enclosed in braces must match the order of members in the structure definition. 3. It is permitted to have a partial initialization.We can initialize only the first few members and leave the remaining blank.The uninitialized members should be only at the end of the list. 4. The uninitialized members will be assigned default values as follows:  Zero for integer and floating point numbers.  ‘0’ for characters and strings.
  • 15.
    What is aUnion?  Unions are a concept borrowed from structures and therefore follow the same syntax as structures.  The major distinction between structure and union is the storage.  In structure, each member has its own storage location, whereas all the members of a union use the same location.  Although a union may contain many members o different types, it can handle only one member at a time.
  • 16.
    Unions (Example1)  Forexample in the following C program, both x and y share the same location. If we change x, we can see the changes being reflected in y. – union test { – int x, y; – }; – int main() – { – union test t; – t.x = 2; // t.y also gets value 2 – printf("After making x = 2:n x = %d, y = %dnn", t.x, t.y); – t.y = 10; // t.x is also updated to 10 – printf("After making y = 10:n x = %d, y = %dnn", t.x, t.y); – return 0; – }
  • 17.
    Unions (Example1) (cont..) Output: – After making x = 2: – x = 2, y = 2 – After making y = 10: – x = 10, y = 10
  • 18.
    Unions (Example2) #include <stdio.h> #include<string.h> union Data { int i; float f; char str[20]; };
  • 19.
    Unions (Example2) (cont…) intmain( ) { union Data data; data.i = 10; printf( "data.i : %dn", data.i); data.f = 220.5; printf( "data.f : %fn", data.f); strcpy( data.str, "C Programming"); printf( "data.str : %sn", data.str); return 0; }
  • 20.
    Unions (Example2) (cont…) Output: data.i: 10 data.f : 220.500000 data.str : C Programming
  • 21.