Structure and Union
Structure and Union
&
UNION
For example, suppose you want to store data about a book.
You might want to store its name (a string), its price (a float)
and number of pages in it (an int).
If data about say 3 such books is to be stored, then we can follow two
approaches:
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
Memory Space Allocation
8000
emp_id
8004
name[20]
8024
salary
8028
address[50]
8078
dept_no
8080
age
8082 employee
Declaring a Structure Variable
A structure has to declared, after the body of
structure has defined. The syntax of declaring a
structure is
struct <struct_name> <variable_name>;
The example to declare the variable for defined
structure “employee”
struct employee e1;
Here e1 variable contains 6 members that are
defined in structure.
Initializing a Structure Members
The members of individual structure variable is initialize
one by one or in a single statement. The example to
initialize a structure variable is
1) struct employee e1 = {1, “Hemant”,12000, “3 vikas
colony new delhi”,10, 35);
2) e1.emp_id=1; e1.dept_no=1
e1.name=“Hemant”; e1.age=35;
e1.salary=12000;
e1.address=“3 vikas colony new delhi ”;
Accessing a Structure Members
The structure members cannot be directly accessed in the
expression.
#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 Structure
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”);
Program to implement the Structure
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);
Program to implement the Structure
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);
getch();
}
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
Array of Structures
C language allows to create an array of variables of structure.
For Example:
struct employee *emp;
It declare a pointer variable “emp” of employee type.
Access the Pointer in Structures
pointer_var_namestructure_member;
For Example:
empname;
Here “name” structure member is accessed through pointer
variable emp.
Passing Structure to Function
The union data type allocate the space equal to space need to
hold the largest data member of union.
union employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
Memory Space Allocation
8000
emp_id, dept_no, age
8002
salary
8004
name
8022
address
8050
Difference between Structures & Union
Declaring structure using normal variable: Declaring structure using pointer variable:
struct student report; struct student *report, rep;
Accessing structure members using normal Accessing structure members using pointer
variable: variable:
report.mark; report -> mark;
report.name; report -> name;
report.average; report -> average;
Using normal variable Using pointer variable
Syntax: Syntax:
union tag_name union tag_name
{ {
data type var_name1; data type var_name1;
data type var_name2; data type var_name2;
data type var_name3; data type var_name3;
}; };
Example: Example:
union student union student
{ {
int mark; int mark;
char name[10]; char name[10];
float average; float average;
}; };
Declaring union using normal variable: Declaring union using pointer variable:
union student report; union student *report, rep;
Accessing union members using normal variable: Accessing union members using pointer variable:
report.mark; report -> mark;
report.name; report -> name;
report.average; report -> average;