Structure and Union
Structure and Union
Structure and Union
&
UNION
Presented By:Lect. Puneet Gupta
G.P.C.G. Patiala
Data Types
C programming language which has the ability to
divide the data into different types. The type of
a variable determine the what kind of values it
may take on. The various data types are
Simple Data type
Integer, Real, Void, Char
Structured Data type
Array, Strings
User Defined Data type
Enum, Structures, Unions
Defining of Structure
A structure has to defined, before it can used. The
syntax of defining a structure is
struct <struct_name>
{
<data_type> <variable_name>;
<data_type> <variable_name>;
..
<data_type> <variable_name>;
};
Example of Structure
The structure of Employee is declared as
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
8022
8024
8074
8076
8078 employee
emp_id
name[20]
salary
address[50]
dept_no
age
e1.dept_no=1
e1.age=35;
e1.salary=12000;
e1.address=3 vikas colony new delhi;
Structure Assignment
The value of one structure variable is assigned
to another variable of same type using
assignment statement. If the e1 and e2 are
structure variables of type employee then the
statement
e1 = e2;
assign value of structure variable e2 to
e1. The value of each member of e2 is assigned
to corresponding members of e1.
Output of Program
Enter the employee id of employee 1
Enter the name of employee Rahul
Enter the salary of employee 15000
Enter the address of employee 4,villa area, Delhi
Enter the department of employee 3
Enter the age of employee 35
Enter the employee id of employee 2
Enter the name of employee Rajeev
Enter the salary of employee 14500
Enter the address of employee flat 56H, Mumbai
Enter the department of employee 5
Enter the age of employee 30
Output of Program
The employee id of employee is : 1
The name of employee is : Rahul
The salary of employee is : 15000
The address of employee is : 4, villa area, Delhi
The department of employee is : 3
The age of employee is : 35
The employee id of employee is : 2
The name of employee is : Rajeev
The salary of employee is : 14500
The address of employee is : flat 56H, Mumbai
The department of employee is : 5
The age of employee is : 30
Array of Structure
C language allows to create an array of
variables of structure. The array of structure is
used to store the large number of similar
records. For example to store the record of
100 employees then array of structure is used.
The method to define and access the array
element of array of structure is similar to other
array. The syntax to define the array of
structure is
Struct
<struct_name>
<var_name>
<array_name> [<value>];
For Example:-
Defining of Union
A union has to defined, before it can used. The
syntax of defining a structure is
union <union_name>
{
<data_type> <variable_name>;
<data_type> <variable_name>;
..
<data_type> <variable_name>;
};
Example of Union
The union of Employee is declared as
union employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
employee
Application of Structures
Structure is used in database management to
maintain data about books in library, items in
store, employees in an organization, financial
accounting transaction in company. Beside that
other application are
1)Changing the size of cursor.
2)Clearing the contents of screen.
3)Drawing any graphics shape on screen.
4)Receiving the key from the keyboard.
Application of Structures
5) Placing cursor at defined position on screen.
6) Checking the memory size of the computer.
7) Finding out the list of equipments attach to
computer.
8) Hiding a file from the directory.
9) Sending the output to printer.
10) Interacting with the mouse.
11) Formatting a floppy.
12) Displaying the directory of a disk.
Summary
A structure is a user defined data type that
groups logically related data items of different
data types into a single unit.
The elements of a structure are stored at
contiguous memory locations.
The value of one structure variable is assigned
to another variable of same type using
assignment statement.
An array of variables of structure is created.
A variable of structure type is defined as a
member of other structure type called nested
structure.
Summary
The member of structure variable is accessed by
pointer variable with arrow operator ().
The structure variable can be passed to a
function as a parameter.
The function can return a variable of structure
type.
A union is like structure that group logically
related variables into a single unit. The union
allocate the space equal to space need to hold
the largest data member of union.
Structure used in database management and
many more applications.
Thanks