5 - Structure and Union
5 - Structure and Union
1. Structure Definition
A structure is a user-defined data type that allows grouping
of different data types under a single name. This grouping
enables the creation of complex data types that reflect real-
world entities.
• Purpose: Structures are used to represent a record,
which is a collection of related data items. For example,
a student record may contain a name (string), age
(integer), and grade (float).
• Benefits: By using structures, programmers can manage
related data efficiently, enhancing code readability and
maintainability.
2. Structure Declaration
Declaring a structure involves defining its components or
members. Each member can be of a different data type, and
they are accessed using the structure variable.
• Components: A structure can include basic data types,
other structures, and even arrays. This flexibility allows
for modeling complex data relationships.
• Visibility: When a structure is declared, memory is
allocated for its members. The size of the structure is
determined by the sum of the sizes of its individual
members, along with potential padding for alignment.
3. Structure Assignments
Structure assignments involve copying the values from one
structure variable to another. This operation copies all
member values from the source structure to the target
structure.
• Operation Details: When assigning one structure to
another, each member of the source structure is
assigned to the corresponding member of the
destination structure.
• Caveats: Care must be taken with assignment operations
to ensure that both structures are of the same type;
otherwise, compilation errors may occur.
4. Arrays in Structures
An array can be a member of a structure, allowing for the
storage of multiple values of the same type within a single
structure. This is particularly useful for handling lists of data.
• Example Applications: In a student structure, an array
could be used to store multiple grades for different
subjects.
• Memory Allocation: When an array is included in a
structure, the memory is allocated for the entire array,
and the structure’s size is adjusted accordingly.
5. Structure Arrays
A structure array is an array where each element is a
structure. This enables the management of multiple records
of the same type in a single array.
• Benefits: Structure arrays are particularly useful for
handling collections of data, such as a list of students,
where each student has the same attributes.
• Accessing Elements: Elements in a structure array can
be accessed using both the array index and the member
operator, allowing for organized data retrieval and
manipulation.
6. Pointer to Structures
Pointers to structures enable the dynamic allocation of
structure variables, allowing for more flexible memory
management.
• Dynamic Memory Management: Using pointers,
programmers can allocate memory for structures at
runtime, which is essential for handling data that may
change in size.
• Pointer Operations: Pointers can be used to access
structure members, enabling efficient data manipulation
and function passing.
7. Nested Structures
A nested structure is a structure that contains another
structure as a member. This allows for the creation of more
complex data types that can represent hierarchical data
relationships.
• Use Cases: For example, a structure representing a
student may contain another structure representing the
address, thus encapsulating related information.
• Accessing Members: To access members of a nested
structure, multiple member operators are used,
reflecting the hierarchy of data.
8. Arrays and Arrays of Structures
Arrays of structures allow for the organization of multiple
structure records in a single array. This approach combines
the benefits of both arrays and structures, facilitating
complex data handling.
• Usage Scenarios: Arrays of structures are commonly
used in applications requiring the management of a list
of records, such as databases or student information
systems.
• Access Patterns: Accessing members of structures in an
array requires specifying both the array index and the
member name, allowing for clear and organized data
retrieval.
9. Union
A union is another user-defined data type that allows storing
different data types in the same memory location. Unlike
structures, where each member has its own memory
allocation, a union allocates memory for the largest member
only.
• Memory Efficiency: Unions are memory-efficient as they
save space when multiple data types are not needed
simultaneously.
• Use Cases: Unions are often used in situations where a
variable can hold different types at different times, such
as in data conversion applications.
10. File Handling with Structures and Unions
File handling is an essential aspect of programming that
allows for the persistent storage of data. Structures and
unions can be used to read from and write to files, making
data management more organized.
• Structure File Handling: Structures can be used to
represent records that are read from or written to files.
By organizing data into structures, file operations
become more manageable.
• Union File Handling: When using unions in file handling,
the programmer must carefully manage the data being
written and read, ensuring that the correct data type is
interpreted.
Conclusion
Understanding structures and unions is vital for BCA students
as they provide the foundation for managing complex data
types in programming. Structures allow for the grouping of
related data, while unions offer memory efficiency when
dealing with multiple data types. By mastering these
concepts, students can enhance their programming skills,
enabling them to design and implement effective data
management solutions.