0% found this document useful (0 votes)
13 views

Structures and Unions in C

The document provides an overview of structures and unions in the C programming language, detailing their definitions, declarations, memory allocation, and usage. It explains how structures can group different data types and how unions allow variables to share the same memory space. Additionally, the document includes examples of implementing structures and unions, along with programs for employee and student details.

Uploaded by

kanimozhisasi345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Structures and Unions in C

The document provides an overview of structures and unions in the C programming language, detailing their definitions, declarations, memory allocation, and usage. It explains how structures can group different data types and how unions allow variables to share the same memory space. Additionally, the document includes examples of implementing structures and unions, along with programs for employee and student details.

Uploaded by

kanimozhisasi345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

UNIT-II

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

 Integer, Real, Void, Char


• Structured Data type

Array, Strings
• User Defined Data type

Enumerations, Structures, Unions


Structure
 A structure is a user defined data type that groups logically
related data items of different data types into a single unit.
All the elements of a structure are stored at contiguous

( neighboring) memory locations.


A variable of structure type can store multiple data items of
different data types under the one name.
As the data of employee in company that is name, Employee
ID, salary, address, phone number is stored in structure data
type.
Declaration 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;
};
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;

printf (“\n Enter the employee id of employee:”);


scanf(“%d”,&e1.emp_id);

printf (“\n Enter the name of employee:”);


scanf(“%s”, &e1.name);

printf (“\n Enter the salary of employee:”);


scanf(“%f”,&e1.salary);

printf (“\n Enter the address of employee:”);


scanf(“%s”,&e1.address);

printf (“\n Enter the department of employee:”);


scanf(“%d”,&e1.dept_no);

printf (“\n Enter the age of employee:”);


scanf(“%d”,&e1.age);
Program to implement the Structure(3)

printf (“\n Enter the employee id of second employee:”);


scanf(“%d”,&e2.emp_id);

printf (“\n Enter the name of second employee:”);


scanf(“%s”,&e2.name);

printf (“\n Enter the salary of second employee:”);


scanf(“%f”,&e2.salary);

printf (“\n Enter the address of second employee:”);


scanf(“%s”,&e2.address);

printf (“\n Enter the department of second employee:”);


scanf(“%d”,&e2.dept_no);

printf (“\n Enter the age of second employee:”);


scanf(“%d”,&e2.age);
Program to implement the Structure(4)
// Display the First Employee Details
printf (“\n The employee id of employee is : %d”, e1.emp_id);
printf (“\n The name of employee is : %s”, e1.name);
printf (“\n The salary of employee is : %f”, e1.salary);
printf (“\n The address of employee is : %s”, e1.address);
printf (“\n The department of employee is : %d”, e1.dept_no);
printf (“\n The age of employee is : %d”, e1.age);

// Display the second Employee Details


printf (“\n The employee id of 2nd employee is : %d”, e2.emp_id);
printf (“\n The name of 2nd employee is : %s”, e2.name);
printf (“\n The salary of 2nd employee is : %f”, e2.salary);
printf (“\n The address of 2nd employee is : %s”, e2.address);
printf (“\n The department of 2nd employee is : %d”, e2.dept_no);
printf (“\n The age of 2nd employee is : %d”,e2.age);
getch();
}
Output of Program
Enter the employee id of employee: 1
Enter the name of employee: Arun
Enter the salary of employee: 30000
Enter the address of employee: 4 Nehru st, Chennai
Enter the department of employee: 3
Enter the age of employee: 35

Enter the employee id of 2nd employee: 2


Enter the name of 2nd employee: Ajith
Enter the salary of 2nd employee: 35500
Enter the address of 2nd employee: flat 56H, Salem
Enter the department of 2nd employee: 5
Enter the age of 2nd employee: 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> <array_name> [<value>];
For Example:

Struct employee e1[100];


Program to implement the Array of 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 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);

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(3)
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);
}

// Desplay the output details

for (i=1; i<=10; i++)


{
printf (“The employee id of employee is : %d”, e[i].emp_id);

printf (“The name of employee is: %s”,e[i].name);


Program to implement the Array of Structure(4)

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
 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

The structure of Employee is declared as


struct employee
{ int emp_id;
char name[20];
float salary;
int dept_no;
struct date
{ int day;
int month;
int year;
} doj;
};
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:
employee.doj.day;
employee.doj.month;
employee.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 by
using the pointer variable with arrow operator()
instead of period operator(.)
 The syntax to access the pointer to structure.
pointer_var_namestructure_member;
 For Example:
empname;

 Here “name” structure member is accessed


through pointer variable emp.
Passing Structure to Function(1)

 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
{
int emp_id;
char name[20];
float salary;
};
Passing Structure to Function(2)
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);

// Calling Function
printdata (struct employee e1);
getch();
}
Passing Structure to Function(3)

// Called Function or Function Definition


void printdata( struct employee emp)
{
printf (“\n The employee id of employee is : %d”, emp.emp_id);
printf (“\n The name of employee is : %s”, emp.name);
printf (“\n The salary of employee is : %f”, emp.salary);
}
Function Returning Structure (1)
 The function can return a variable of structure
type like a integer and float variable. The
program to return a structure from function.

#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

printf (“\nThe employee id of employee is : %d”, emp.emp_id);

printf (“\nThe name of employee is : %s”, emp.name);

printf (“\nThe salary of employee is : %f”, emp.salary);

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;

printf (“\n Enter the number of Students:”);


scanf(“%d” ,&n);

for(i=0;i<n;i++)
{

printf (“\n Enter the Student Roll No:”);


scanf(“%d”, &st[i].roll_no);

printf (“\n Enter the Student Name:”);


scanf(“%s”,&st[i].name);

printf (“\n Enter the Student Mark1:”);


scanf(“%d”,&st[i].mark1);

printf (“\n Enter the Student Mark2:”);


scanf(“%d”,&st[i].mark2);

printf (“\n Enter the Student Mark3:”);


scanf(“%d”,&st[i].mark3);

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’;
}

printf(“ \nRoll No \t name \t Mark1 \t mark2 \t mark3 \t Total \t Average \t Grade”);

for(i=0;i<n; i++)

Printf(“\n %d \t %s \t %d \t %d \t %d \t %d \t %.2f \t %c” , st[i].roll_no, st[i].name,


st[i].mark1, st[i].mark2, st[i].mark3, st[i].total, st[i].average, st[i].grade);
}
Structure: Assignment
1. Program to implement the Structure for employee details
2. Program to implement the Structure for Students details
3. Program to implement the Array of Structure for employee details
4. Program to implement the Structure within Structure for employee
details
5. Program to implement Passing Structure to Function for
employee details
6. Program to implement passing array of structure to a function and
return its value to structure
7. Program to implement Students Grades using Structure.
UNION DATA TYPE
Union Data Type

 A union is a user defined data type like structure.


 The union groups logically related variables into a single unit.
 The union data type allocate the space equal to space need
to hold the largest data member of union.
 The union allows different types of variable to share same
space in memory.
 There is no other difference between structure and union
than internal difference.
 The method to declare, use and access the union is same as
structure.
Declaration 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;
};
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;

printf ("\n Enter the employee id of employee:");


scanf("%d",&e1.emp_id);

printf ("\n Enter the name of employee:");


scanf("%s", &e1.name);

// Display the First Employee Details


printf ("\n The employee id of employee is : %d", e1.emp_id);

printf ("\n The name of employee is : %s", e1.name);

getch();
}
Difference between Structures & Union

1) The memory occupied by structure variable


is the sum of sizes of all the members but
memory occupied by union variable is equal
to space hold by the largest data member of
a union.
2) In the structure all the members are
accessed at any point of time but in union
only one of union member can be
accessed at any given time.
Application of Structures(1)

 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(2)

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.
Union: Assignment

1. Program to print marks, total, average and grade of

students using union.

2. Program to pass Union to function for displaying

students records.

3. Program to implement the Students Record using

Union.
Thanks

You might also like