Structures and Unions in C
Structures and Unions in C
STRUCTURES
&
UNIONS IN C
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
Array, Strings
• User Defined Data type
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
Memory Space Allocation
8000 emp_id
8002 name[20]
8022 salary
8024
address[50]
8074 dept_no
8076
age
Declaring a Structure Variable
A structure variable has to declared, after the
body of structure has defined.
The syntax of declaring a structure variable 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
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
struct employee e1 = {1, “Arun”,30000,
“3 Nehru colony Salem”,10, 30};
e1.emp_id=1; e1.dept_no=1
e1.name=“Arun”; e1.age=30;
e1.salary=30000;
e1.address=“3 Nehru colony Salem”;
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.
Program to implement the Structure(1)
#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(2)
void main ( )
{ struct employee e1,e2;
#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(2)
void main ( )
{
struct employee e1[10];
int i;
for (i=1; i<=10; i++)
{
printf (“Enter the employee id of employee”);
scanf (“%d”,&e[i].emp_id);
getch();
}
Structures within Structures
C language define a variable of structure type as a
member of other structure type.
The syntax to define the structure within structure
is
struct <struct_name>{
<data_type> <variable_name>;
struct <struct_name>
{ <data_type> <variable_name>;
……..} <struct_variable>;
<data_type> <variable_name>;
};
Example of Structure within Structure
// Calling Function
printdata (struct employee e1);
getch();
}
Passing Structure to Function(3)
#include <stdio.h>
#include <conio.h>
struct employee
{
int emp_id;
char name[20];
float salary;
};
Function Returning Structure(2)
void main ( )
{
struct employee emp;
emp = getdata(); // Calling Function
getch();
}
Function Returning Structure (3)
struct employee getdata( )
{
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);
return(e1); }
/*Program to Implement Students grades
using structure */
#include <stdio.h>
#include <conio.h>
struct Student
{
int roll_no;
char name[20];
int mark1,mark2,mark3,total;
float average;
char grade;
};
void main ( )
{ struct student st[20];
int i, n;
for(i=0;i<n;i++)
{
st[i].total=st[i].mark1+st[i].mark2+st[i].mark3;
st[i].average= st[i].total/3;
if(st[i].average<50)
st[i].grade =‘D’;
else if(st[i].average<60)
st[i].grade =‘C’;
else if(st[i].average<70)
st[i].grade =‘B’;
else if(st[i].average<80)
st[i].grade =‘A’;
else
st[i].grade =‘S’;
}
for(i=0;i<n; i++)
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;
};
Memory Space Allocation
8000
emp_id, dept_no, age
8002
salary
8004
name
8020
address
8050
union employee
{ /* Program to implement employee details using Union*/
int emp_id;
char name[20];
};
main ( )
{
union employee e1;
getch();
}
Difference between Structures & Union
students records.
Union.
Thanks