CH 05- structure and union.pptx
CH 05- structure and union.pptx
STRUCTURE
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,
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;
};
Memory Space Allocation
800 emp_id
0 name[20
800 ]
2
802 salary
2 address[5
802 0]
4
807 dept_n
4 o
807 age
Declaring a Structure Variable
A structure has to declared, after the body of structure has
defined. The syntax of declaring a structure is
2) e1.emp_id=1;
e1.name=“Hemant”; e1.dept_no=
1
e1.salary=12000;
e1.age=35;
e1.address=“ LPU
JALANDHAR”;
Accessing a Structure Members
The structure members cannot be directly accessed in the
expression. They are accessed by using the name of
structure variable followed by a dot and then the name
of member variable. The method used to access the
structure variables are e1.emp_id, e1.name, e1.salary,
e1.address, e1.dept_no, e1.age. The data with in the
structure is stored and printed by this method using scanf
and printf statement in c program.
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.
#include<stdio.h>
#include<conio.h>
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
void main ( )
{ struct employee e1, e2;
printf (“Enter the employee id of employee”);
scanf(“%d”,&e1.emp_id);
printf (“Enter the name of employee”);
scanf(“%s”,e1.name);
printf (“Enter the salary of employee”);
scanf(“%f”,&e1.salary);
printf (“Enter the address of employee”);
scanf(“%s”,e1.address);
printf (“Enter the department of employee”);
scanf(“%d”,&e1.dept_no);
printf (“Enter the age of employee”);
scanf(“%d”,&e1.age);
printf (“Enter the employee id of employee”);
scanf(“%d”,&e2.emp_id);
printf (“Enter the name of employee”);
scanf(“%s”,e2.name);
printf (“Enter the salary of employee”);
scanf(“%f”,&e2.salary);
printf (“Enter the address of employee”);
scanf(“%s”,e2.address);
printf (“Enter the department of employee”);
scanf(“%d”,&e2.dept_no);
printf (“Enter the age of employee”);
scanf(“%d”,&e2.age);
printf (“The employee id of employee is : %d”, e1.emp_id);
printf (“The name of employee is : %s”, e1.name);
printf (“The salary of employee is : %f”, e1.salary);
printf (“The address of employee is : %s”, e1.address);
printf (“The department of employee is : %d”, e1.dept_no);
printf (“The age of employee is : %d”, e1.age);
Program to implement the Structure
printf (“The employee id of employee is : %d”,
e2.emp_id);
printf (“The name of employee is : %s”,
e2.name);
printf (“The salary of employee is : %f”,
e2.salary);
printf (“The address of employee is : %s”,
e2.address);
printf (“The department of employee is : %d”,
e2.dept_no);
printf (“The age of employee is : %d”,e2.age);
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,
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
For Example:-
Struct employee e1[100];
Program to implement the Array of Structure
#include <stdio.h>
#include <conio.h>
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
Program to implement the Array of Structure
void main ( )
{ struct employee e1[5]; int i,n;
for (i=1; i<=100; i++)
{
printf(“enter the no of employees”);
scanf(“%d”,&n);
printf (“Enter the employee id of employee”);
scanf (“%d”,&e[i].emp_id);
printf (“Enter the name of employee”);
scanf (“%s”,e[i].name);
printf (“Enter the salary of employee”);
scanf (“%f”,&e[i].salary);
Program to implement the Array of Structure
printf (“Enter the address of employee”);
scanf (“%s”, e[i].address);
printf (“Enter the department of employee”);
scanf (“%d”,&e[i].dept_no);
printf (“Enter the age of employee”);
scanf (“%d”,&e[i].age);
}
for (i=1; i<=100; i++)
{
printf (“The employee id of employee is : %d”,
e[i].emp_id);
Program to implement the Array of Structure
printf (“The salary of employee is:
%f”, e[i].salary);
printf (“The address of employee is :
%s”, e[i].address);
printf (“The department of employee is :
%d”, e[i].dept_no);
printf (“The age of employee is : %d”,
e[i].age);
}
getch();
}
Structures within Structures
- structure can be nested in two ways-
- By separate structures
- By Embedded structures
e1.doj.dd
e1.doj.mm
Accessing Structures within Structures
The data member of structure within structure
is accessed by using two period (.) symbol.
The syntax to access the structure within
structure is
struct _var. nested_struct_var. struct_member;
For Example:-
e1.doj.day;
e1.doj.month;
e1.doj.year;
Pointers and Structures
C language can define a pointer variable of
structure type. The pointer variable to structure
variable is declared by using same syntax to
define a pointer variable of data type. The
syntax to define the pointer to structure
struct <struct_name> *<pointer_var_name>;
For Example:
struct employee *emp;
It declare a pointer variable “emp” of employee
type.
Access the Pointer in Structures
The member of structure variable is accessed
byusing the pointer variable with arrow
operator() instead of period operator(.). The
syntax to access the pointer to structure.
pointer_var_namestructure_member;
For Example:
empname;
Here“name” structure member is accessed
through pointer variable emp.
Passing Structure to Function
The structure variable can be passed to a
function as a parameter. The program to pass
a structure variable to a function.
#include <stdio.h>
#include <conio.h>
struct employee
{
intemp_id;
char name[20];
float salary;
};
Passing Structure to Function
void main ( )
{
struct employee e1;
printf (“Enter the employee id of employee”);
scanf(“%d”,&e1.emp_id);
printf (“Enter the name of employee”);
scanf(“%s”,e1.name);
printf (“Enter the salary of employee”);
scanf(“%f”,&e1.salary);
printdata (struct employee e1);
getch();
Passing Structure to Function
the different data types just like structure. But unlike structures, all the members in
the C union are stored in the same memory location. Due to this, only one member
Syntax:
union union_name {
datatype member1;
datatype member2;
...
};
Union example
// C Program to demonstrate how to use union
#include <stdio.h>
// union template or declaration
union un {
int member1;
char member2;
float member3;
}; Output:
The value stored in
// driver code member1 = 15
int main()
{
// defining a union variable
union un var1;
// initializing the union member
var1.member1 = 15;
printf("The value stored in member1 = %d",
var1.member1);
return 0;
}
Difference between Structure & Union
Parameters Structure Union
Keyword struct union
Memory Allocation For all the structure For only one member
members who is largest
Example struct emp union emp
{ {
int age; int age;
char name[50]; char name[50];
float sal; float sal;
}; };
Memory occupied More memory space Less memory space
Memory access Allows to access all Allows to access only
the members at any one member at a time
time
Thank
s