CCCS - 206 Unit - 2 Structures, Unions and File Handling
CCCS - 206 Unit - 2 Structures, Unions and File Handling
CCCS - 206 Unit - 2 Structures, Unions and File Handling
● Construct individual arrays for storing names, roll numbers, and marks.
● Use a special data structure to store the collection of different data types.
Syntax of Structure
The ,struct keyword is used to define the structure. Let's see the syntax to define the structure in c.
Example of Structure
Let's see the example to define a structure for an entity employee in c.
Memory Allocation of Structure
Declaring a Structure Variable
We can declare a variable for the structure so that we can access the member of the structure easily. There are two
ways to declare structure variable:
Nested Structures The embedded structure enables us to declare the structure inside the
structure. Hence, it requires less line of codes but it can not be used in
multiple data structures. Consider the following example.
The structure can be nested in the following ways.
1. By separate structure
2. By Embedded structure
1) Separate structure
Array refers to a collection consisting of elements of Structure refers to a collection consisting of elements of
homogeneous data type. heterogeneous data type.
Array uses subscripts or “[ ]” (square bracket) for Structure uses “.” (Dot operator) for element access
element access
Array declaration is done simply using [] and not any Structure declaration is done with the help of “struct”
keyword. keyword.
Array traversal and searching is easy and fast. Structure traversal and searching is complex and slow.
Array elements are accessed by their index number Structure elements are accessed by their names using
using subscripts. dot operator.
Pointers to Structures
Pointers to Structures
Nested Structures
Unions
Unions can be defined as a user-defined data type which is a
collection of different variables of different data types in the same
memory location. The union can also be defined as many
members, but only one member can contain a value at a particular
point in time.
2. Binary files
1. Text files
Text files are the normal .txt files. You can easily create text Instead of storing data in plain text,
files using any simple text editors such as Notepad. they store it in the binary form (0's
and 1's).
When you open those files, you'll see all the contents within
the file as plain text. You can easily edit or delete the contents. They can hold a higher amount of