Unit IV: Structures and Unions
1. Basics of Structures
A Structure is a user-defined data type in C that allows you to group variables of different types under a single name.
It is used to represent a record.
- Processing of Structures: Involves defining the structure template, declaring structure variables, and accessing members.
- Member Access: Members are accessed using the dot operator (.) for variable names.
- Arrays of Structures: You can create an array where each element is a structure, ideal for storing multiple records like student or employee data.
2. User Defined Data Types (typedef)
The typedef keyword is used to create an alias for an existing data type.
In structures, it simplifies the declaration by removing the need to repeatedly use the struct keyword.
Example: typedef struct { int x; int y; } Point;
3. Structures and Pointers
Pointers to structures allow for efficient memory management and dynamic data structures.
- Pointers to Structures: A pointer can hold the address of a structure variable.
- Arrow Operator (->): When using a pointer to access structure members, the arrow operator is used instead of the dot operator.
4. Structures and Functions
Structures can be passed to functions to perform specific operations on complex data.
- Passing Structures: You can pass an entire structure, individual members, or a pointer to the structure to a function.
- Efficiency Note: Passing a pointer is generally more efficient as it avoids copying the entire structure.
5. Self-referential Structures & Table Lookup
- Self-referential Structures: A structure that contains a pointer to an instance of itself.
This is the foundation for Linked Lists and Trees.
- Table Lookup: This involves using structures to manage data in tabular formats, often used in compiler design for symbol tables.
6. Unions
A Union is a special data type that allows storing different data types in the same memory location.
- Memory Efficiency: Unlike structures, a union only allocates enough memory for its largest member.
- Usage: Only one member can contain a value at any given time.
7. Problem Solving with Structures and Unions
This section focuses on writing programs to solve real-world problems using these constructs.
- Implementing database-like records (e.g., library systems).
- Managing memory-constrained systems using Unions.
Exam Tips
- Struct vs Union: Always remember that in a
struct, all members have their own memory, but in a union, all members share the same memory.
- Pointer Access: Use
(*ptr).member or the shorthand ptr->member when dealing with pointers.
- Size Calculation: The size of a structure is at least the sum of the sizes of its members, while the size of a union is the size of its largest member.
Frequently Asked Questions
Q: What is a self-referential structure?
It is a structure that has a pointer to a structure of its own type. It's used to link data together in structures like linked lists.
Q: Why use typedef with structures?
It makes the code cleaner and easier to read by creating shorter names for complex structure types.