0% found this document useful (0 votes)
5 views27 pages

Unit 6 Structure and Union

Uploaded by

omgallery39
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)
5 views27 pages

Unit 6 Structure and Union

Uploaded by

omgallery39
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/ 27

Department of FOT

01CE0101 - Computer Unit - 6


Structure and union
Programming
Structures
 Arrays allow to define type of
variables that can hold several
data items of the same kind.
 Similarly structure is another
Structur user defined data type available
e in C that allows to combine
data items of different kinds.
 Structures are used to represent a
record.
 To define a structure, you must use
the struct keyword.
 The struct keyword defines a new data type,
Defining a with more than one member.
Structure  The format of the struct statement is as follows
Example
 To access any member of a structure,
Accessin we use the member access
g operator (.)
Structur  The member access operator is
e coded as a period between the
structure variable name and the
Member structure member that we wish to
s access.
 You would use the keyword struct to
define variables of structure type.
Example
Program
Example
Program
Program
output
 You can pass a structure as a function
Structure argument in the same way as you pass
any other variable or pointer.
s as
Function
Argumen
ts
 You can pass a structure as a function
argument in the same way as you pass any
other variable or pointer(Call by Value).

Program
 You can pass a structure as a function
argument in the same way as you pass any
other variable or pointer(Call by Reference).

Program
Union
 A union is a special data type
available in C that allows to store
different data types in the same
memory location.
 You can define a union with many
Union members, but only one member can
contain a value at any given time.
 Unions provide an efficient way of
using the same memory location for
multiple-purpose.
 To define a union, you must use
the union keyword in the same way as
you did while defining a structure.
Defining a  The union statement defines a new data
Union
type with more than one member for
your program.
 The format of the union statement is as
follows

Defining
a Union
 Each member definition is a normal
variable definition, such as int i; or float
f; or any other valid variable definition.
 At the end of the union's definition,
before the final semicolon, you can
specify one or more union variables but
it is optional.
Defining • Now, a variable of Data type can
store an integer, a floating-point
a Union number, or a string of characters.
• It means a single variable, i.e.,
same memory location, can be used
to store multiple types of data.
Example
 To access any member of a union, we
use the member access operator (.).
 The member access operator is coded
Accessing
as a period between the union variable
Union
name and the union member that we
Members
wish to access.
 You would use the keyword union to
define variables of union type.
Example
program
Output
of
program
Difference
between
structure
and union
• Structures gather more than one piece of
data about the same subject together in the
same place.
• It is helpful when you want to gather the data
of similar data types and parameters like first
Advantag name, last name, etc.
es of • It is very easy to maintain as we can
represent the whole record by using a single
structure name.
• In structure, we can pass complete set of
records to any function using a single
parameter.
• You can use an array of structure to store
more records with similar types.
 Change of one data structure in a code necessitates
changes at many other places. Therefore, the
changes become hard to track.
 Structure is slower because it requires storage
space for all the data.
Disadvantages  You can retrieve any member at a time in
of structure structure whereas you can access one member at a
time in the union.
 Structure occupies space for each and every
member written in inner parameters while union
occupies space for a member having the highest size
written in inner parameters.
 Structure supports flexible array. Union does not
support a flexible array.
• It occupies less memory compared to
structure.
• When you use union, only the last
variable can be directly accessed.
Advantages of • Union is used when you have to use
union the same memory location for two or
more data members.
• It enables you to hold data of only one
data member.
• Its allocated space is equal to
maximum size of the data member.
• You can use only one union member
at a time.
Disadvantages • All the union variables cannot be
of union initialized or used with varying
values at a time.
• Union assigns one common storage
space for all its members.
That’s All….

Thank you!!!

You might also like