PSPC Unit IVa Structures
PSPC Unit IVa Structures
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
Example of Structure
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
8022
8024 salary
address[50]
8074
8076
dept_no
8078 employee
age
1) e1.emp_id=1; e1.dept_no=1
e1.name=“Hemant”; e1.age=35;
e1.salary=12000;
e1.address=“3 vikas colony new delhi”;
10
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.
11
#include <stdio.h>
#include <conio.h>
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
12
13
14
15
16
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
17
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
18
Array 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:-
Struct employee e1[100];
We can access as: e[i].emp_id
19
20
21
22
23
24
25
26
27
28
29
30
31
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>;
};
32
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;
};
33
8022
address
8050
employee
34
35
Application of Structures
36
Typedef
37
THANKS
38