Unit 6Structures and Unions
Follows IGNOU MCS-201, Unit 6
An array groups many values of the same type, but real-world records mix types: a student has a roll number (integer), a name (string) and marks (numbers). A structure groups such related items of different types under one name, so the whole record can be handled as a single unit. A union looks similar but lets several members share the same memory. This unit covers how to declare, initialise and use both.
In this unit you will learn to
- Declare structure types and create structure variables, with and without
typedef. - Access members with the dot operator and initialise structures, including nested ones.
- Assign whole structures, and explain why they cannot be compared with
==and whysizeofmay show padding. - Pass structures to functions and return them from functions.
- Build arrays of structures and structures that contain arrays.
- Declare and use unions, and explain how they differ from structures.
Sections in this unit
- 6.1Declaring StructuresLearn what a structure is, how to declare a structure type and create variables of it, and how typedef gives the type a shorter name.9 min
- 6.2Accessing and Initialising Structure MembersAccess members with the dot operator, initialise plain and nested structures, copy whole structures, and understand comparison and padding.13 min
- 6.3Structures as Function ArgumentsPass structures to functions by value, return structures from functions, and use compound literals for constant structure arguments.12 min
- 6.4Structures and ArraysStore many records in an array of structures, use arrays as structure members, and process records with loops.9 min
- 6.5UnionsDeclare, initialise and access unions, understand that their members share one block of memory, and compare unions with structures.12 min