Chapter13: Structures and Unions
This is one of the most powerful and useful features in C. Structures let you create your own custom data type by grouping different types of data together – like a real-world object.
Think of a structure as a record or form that holds multiple pieces of information about one thing (like a student: name, roll number, marks, grade).
Unions are similar but save memory by sharing the same memory space.
Let’s learn everything in detail.
1. Defining Structures
Syntax:
|
0 1 2 3 4 5 6 7 8 9 10 |
struct structure_name { data_type member1; data_type member2; // ... more members }; |
This creates a new data type called struct structure_name.
Example – Defining a Student Structure
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> // Define the structure (blueprint) struct Student { char name[50]; // String (character array) int roll_number; float marks; char grade; }; int main() { // We will use it in next examples return 0; } |
2. Declaring Structure Variables
You can declare variables in three ways:
Way 1: After definition
|
0 1 2 3 4 5 6 |
struct Student s1; // s1 is a variable of type struct Student |
Way 2: While defining structure (most common)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
struct Student { char name[50]; int roll_number; float marks; char grade; } s1, s2; // s1 and s2 are variables |
Way 3: Using typedef (very popular – makes code cleaner)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
typedef struct { char name[50]; int roll_number; float marks; char grade; } Student; Student s1, s2; // Now no need to write "struct" every time |
3. Accessing Structure Members
Two operators:
- Dot (.) → when you have the structure variable directly
- Arrow (->) → when you have a pointer to the structure
Example – Using Dot Operator
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <stdio.h> #include <string.h> typedef struct { char name[50]; int roll_number; float marks; char grade; } Student; int main() { Student s1; // Assign values strcpy(s1.name, "Alex"); s1.roll_number = 101; s1.marks = 92.5; s1.grade = 'A'; // Access and print printf("Student Details:\n"); printf("Name : %s\n", s1.name); printf("Roll Number : %d\n", s1.roll_number); printf("Marks : %.2f\n", s1.marks); printf("Grade : %c\n", s1.grade); return 0; } |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 |
Student Details: Name : Alex Roll Number : 101 Marks : 92.50 Grade : A |
4. Nested Structures
A structure inside another structure.
Example – Student with Date of Birth
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
typedef struct { int day; int month; int year; } Date; typedef struct { char name[50]; int roll_number; Date dob; // Nested structure float marks; } Student; int main() { Student s1; strcpy(s1.name, "Maria"); s1.roll_number = 102; s1.dob.day = 15; s1.dob.month = 8; s1.dob.year = 2005; s1.marks = 88.75; printf("Name: %s\n", s1.name); printf("DOB : %02d-%02d-%d\n", s1.dob.day, s1.dob.month, s1.dob.year); return 0; } |
Output:
|
0 1 2 3 4 5 6 7 |
Name: Maria DOB : 15-08-2005 |
5. Array of Structures
Array where each element is a structure – very useful for storing many records.
Example – Store 3 Students
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <stdio.h> #include <string.h> typedef struct { char name[50]; int roll_number; float marks; } Student; int main() { Student class[3]; // Array of 3 students int i; // Input for (i = 0; i < 3; i++) { printf("\nEnter details for student %d:\n", i+1); printf("Name: "); scanf("%s", class[i].name); // or use fgets for full name printf("Roll Number: "); scanf("%d", &class[i].roll_number); printf("Marks: "); scanf("%f", &class[i].marks); } // Output printf("\n--- Class Details ---\n"); for (i = 0; i < 3; i++) { printf("Student %d:\n", i+1); printf(" Name : %s\n", class[i].name); printf(" Roll : %d\n", class[i].roll_number); printf(" Marks : %.2f\n", class[i].marks); } return 0; } |
6. Pointers to Structures
Use arrow operator (->) to access members.
Example:
|
0 1 2 3 4 5 6 7 8 9 10 |
Student s1 = {"John", 103, 95.5}; Student *ptr = &s1; printf("Name: %s\n", ptr->name); // Same as (*ptr).name printf("Marks: %.2f\n", ptr->marks); |
7. Unions
Union is similar to structure, but all members share the same memory location. Only one member can hold value at a time (saves memory).
Syntax:
|
0 1 2 3 4 5 6 7 8 9 10 |
union union_name { data_type member1; data_type member2; // ... }; |
Example – Union vs Structure
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <stdio.h> struct Data { int i; float f; char c; }; union DataUnion { int i; float f; char c; }; int main() { struct Data s; union DataUnion u; printf("Size of struct: %zu bytes\n", sizeof(s)); // Usually 12 bytes printf("Size of union : %zu bytes\n", sizeof(u)); // Only 4 bytes (largest member) // In union – only one value at a time u.i = 65; printf("Union int: %d\n", u.i); // 65 u.c = 'A'; printf("Union char: %c\n", u.c); // A (overwrites previous int) printf("Union int now: %d\n", u.i); // Now garbage or 65 (depends on system) return 0; } |
Use Cases of Union:
- When you know only one type of data will be used at a time (e.g., variant types)
- Memory-critical programs (embedded systems)
- Type punning (advanced – be careful)
Today’s Homework
- Create a structure Book with members: title, author, price, pages. Take input for 3 books and print them.
- Make an array of 5 Employee structures (name, id, salary) and find the employee with highest salary.
- Create a nested structure Person with Address (street, city, pin) inside it.
- Write a program using union to store either an integer or a float (user chooses) and print the value.
- Use pointer to structure to change a student’s marks.
