Home/Notes/Structures & Unions
Board Exam Notes

Structures & Unions Notes

Questions

1–2 questions per semester exam

Difficulty

Medium

Importance

Essential for core C programming modules

Overview

Structures and Unions are user-defined data types in C that allow grouping heterogeneous and homogeneous elements respectively under a single name. Mastering these is crucial for data organization in C programming, appearing frequently in semester exams through questions on memory layout, syntax, and comparative analysis. The core concept to grasp is the distinction between sequential memory allocation in structures versus memory overlaying in unions.

Structure Declaration and Initialization

A structure is a collection of variables of different data types grouped under a single name using the 'struct' keyword. It is primarily used to represent real-world entities where grouping related data is necessary for better maintainability and code clarity.

  • Uses the struct keyword to define a template.
  • Each member has its own distinct memory location.
  • Total size is at least the sum of sizes of all individual members.
  • Access members using the dot (.) operator.
  • Supports initialization during declaration using curly braces {}.

Nested Structures

Nested structures occur when a structure is declared within another structure, allowing for hierarchical data representation. This is essential for modeling complex entities such as a student having both a date of birth and an address structure.

  • A structure within another structure.
  • Inner structure members are accessed using nested dot operators.
  • Syntax follows struct within struct definition.
  • Improves data modularity for complex objects.
  • Memory size is the sum of all members including nested ones.

Unions vs Structures

While both user-defined types look similar, they differ fundamentally in memory allocation: structures allocate memory for all members, whereas unions allocate memory only for the largest member. This makes unions highly efficient for tasks where only one value is required at any given time.

  • Structure memory = Sum of all member sizes.
  • Union memory = Size of the largest member.
  • Structure members can be accessed simultaneously.
  • Union members share the same memory location.
  • Writing to one union member overwrites existing data in others.

Exam Tip

Always draw a memory block diagram showing the difference in space allocation between a struct and a union to secure full marks in comparative questions.

Common Mistakes

  • Assuming unions store all members simultaneously like structures.
  • Forgetting to use the semicolon after closing a structure/union definition brace.
  • Using the dot operator on a structure pointer instead of the arrow (->) operator.

More Revision Notes

Ready to test yourself?

Play topic-wise Structures & Unions questions in Aspirant Arcade — gamified MCQ practice.

Download Free