Unit 4: Structures and Unions
Table of Contents
4.1 Structures: Basics and Processing
A structure is a user-defined data type that groups together related variables of different data types.
Basics of Structures
// 1. Definition struct Student { char name[50]; int rollNumber; float gpa; }; int main() { // 2. Variable declaration struct Student s1; // 3. Processing (Accessing members with dot .) strcpy(s1.name, "Alex"); s1.rollNumber = 101; s1.gpa = 8.5; printf("Name: %s\n", s1.name); return 0; }User-defined data types (typedef)
typedef creates an alias (a new name) for an existing data type. It simplifies structure declarations.
// Using typedef with struct typedef struct { char name[50]; int rollNumber; float gpa; } Student; // 'Student' is now the new type name int main() { Student s1; // Much cleaner than 'struct Student s1' s1.rollNumber = 101; }4.2 Structures with Pointers and Functions
Structures and Pointers
You can have a pointer to a structure. To access members from a structure pointer, you use the arrow operator (->).
Student s1; Student *ptr = &s1; // ptr points to s1 // Accessing members ptr->rollNumber = 102; // Same as (*ptr).rollNumber strcpy(ptr->name, "Maria");s.member (if s is a structure) vs. ptr->member (if ptr is a pointer to a structure). This is a very common point of confusion.Structures and Functions
It's highly recommended to pass structures to functions using pointers to avoid the inefficiency of copying the entire structure (which is the default pass-by-value behavior).
// Good practice: Pass by pointer void printStudent(Student *s) { printf("Name: %s\n", s->name); printf("Roll: %d\n", s->rollNumber); } int main() { Student s1 = {"Alex", 101, 8.5}; printStudent(&s1); // Pass the address }4.3 Arrays of Structures and Self-Reference
Arrays of Structures
You can create an array where each element is a structure. This is perfect for storing records.
Student class[60]; // An array of 60 Student structures class[0].rollNumber = 101; strcpy(class[0].name, "Alex");This is often used for Table Lookup, where you search the array for a record matching a key.
Pointers to Structures
This concept (covered in 4.2) is fundamental to managing arrays of structures and self-referential structures.
Self-referential Structures
A structure that contains a pointer to itself (of the same structure type). This is the fundamental building block for data structures like Linked Lists and Trees.
// A node in a linked list typedef struct Node { int data; struct Node *next; // Pointer to the next node in the list } Node;4.4 UNIONS
A union is a user-defined type where all members share the same memory location. Only one member can be active and contain a value at any given time.
typedef union { int i; float f; char c; } Data; int main() { Data d; d.i = 10; printf("i: %d\n", d.i); // Prints 10 d.f = 220.5; printf("f: %.1f\n", d.f); // Prints 220.5 // Previous 'i' value is now corrupted! printf("i: %d\n", d.i); // Prints garbage/corrupted value }4.5 Problem Solving with Structures and Unions
Structure vs. Union: Key Difference
struct: Stores ALL members. Memory size is the SUM of all member sizes (plus padding). Use when you need to store multiple values together.union: Stores only ONE member at a time. Memory size is the size of the LARGEST member. Use when you need to store one of several possible types in a single space.