4.2 Structures & Unions in C
v User defined data types
ØThe
data types which are created by the user with the help of basic data types are
known as user defined data types.
Ø
It uses
Basic or predefined or Primary data types are int , char , float , double ,
void.
Ø
C
allows us to create or define our own data types. This is done for the purpose
of customized programming and to reduce the complexity of programs.
Ø
In C,
user defined data types can be created in three different ways.
1.
Structures
2.
Unions
3.
Enumerations
1) Structures: -
Ø A structure in C is a user-defined data type
that allows us to combine variables of different data types under a single
name.
Ø Structures help organize complex data like
student or employee details
Ø The struct keyword is used to define a
structure.
Ø It represents a record (e.g., student name, roll
number, marks).
Example: A structure can
store a student’s name (string), roll number (integer), and marks (float)
together.
Ø
The
size of the structure variable is equals to the sum of all the sizes of the
members in a structure.
Ø
These
are used when different items are to be grouped under one category.
Ø
Changing
one member’s value does not affect other members.
Ø
Members
are declared inside { } braces of the struct block. The closing brace of
a structure ends with a semicolon (;).
Ø
Structure
members are accessed using the instance variable (e.g., variable.member). Here we
use dot (.) operator is called member selection operator.
Declaring Structure Variables
·
After defining a
structure, you can declare variables of that structure type.
·
You can declare them:
1. Separately, after the structure definition,
2. Together, within the definition.
Accessing Structure Members
·
We can initialize a
structure at the time of declaration, just like arrays.
·
The dot operator
(.) is used to access structure members through a structure variable.
Example:
-
Output:
-
v Arrays of Structures: -
Ø
The
collection of structure variables with same structure type are grouped together
is called array of structure.
Ø
Structure
is used to store the information of one particular object but if we need to
store such 100 objects then Array of Structure is used.
Ø
An
array is a collection of similar type; therefore an array can be of structure
type.
Example: -
Output: -
v Structures within Structures: -
Ø
A
structure within a structure is called as a “nesting of structure”. This
enables to access the members of inner structure with outer structure
variables.
Ø
We
can also have variables of type inner structure anywhere in the program.
Example: -
Output: -
v Unions: -
Ø A union in C is a user-defined data type (like struct)
that can store different data types in the same memory location.
Ø All members of a union share single common memory.
At any moment, a union can hold a value for only one of its members, the one
you most recently stored.
Ø
It is
identified by a key word called ‘union’.
Ø
Size
of the union is equal to size of the biggest data types among the members.
Ø
Unions
are used when one among the member are to be used.
Ø
All
the members of a union should not be used at a time since they share the same
memory area.
Ø
Changing
one member’s value do affect other members.
Ø
Members
are declared inside { } braces of the union block. The closing brace of
a union ends with a semicolon (;).
Ø
union
members are accessed using the instance variable (e.g., variable.member). Here we
use dot (.) operator is called member selection operator.
Example: -
Output: -
|
Feature |
Structure |
Union |
|
Definition |
Collection of variables of different types |
Collection of variables sharing the same memory |
|
Memory |
Each
member has its own memory space |
All
members share the same memory space |
|
Size |
Sum of all member sizes |
Size of the largest member |
|
Access |
All
members can be used at once |
Only
one member can be used at a time |
|
Keyword |
struct |
union |