Up to this point, you have learned several important tools in C:
Variables store individual values
Arrays store collections of values
Pointers give access to memory addresses
Strings allow us to work with text
Now we take a step back and look at how programmers organize data more effectively.
This is where data structures come in.
Data structures help us store, organize, and manage data so that programs become easier to build and more efficient to run.
1. What Is a Data Structure?
A data structure is:
A way of organizing and storing data so it can be used efficiently.
Different problems require different ways of organizing data.
For example:
A list of numbers → array
Student records → structure
Dynamic collections → linked lists
Choosing the right data structure makes programs faster and easier to maintain.
2. Basic Data Types
Before we build complex structures, we start with basic types.
These include:
int
float
char
double
Example:
int age = 20;
float price = 19.99;
char grade = 'A';
Each of these stores a single value.
But real programs often need to store many related values.
That’s where derived data structures come in.
3. Derived Data Structures
Derived structures are built using basic data types.
Common derived structures in C include:
Arrays
Structures (struct)
Pointers
Unions
You have already seen arrays and pointers.
Now we will introduce structures and unions.
4. Structures (struct)
A structure groups multiple variables under one name.
Unlike arrays, the variables can be different types.
Example:
struct Student {
char name[50];
int id;
float gpa;
};
This structure represents a student.
It contains:
Now we can create a student variable.
struct Student s1;
And assign values:
strcpy(s1.name, "Alice");
s1.id = 1001;
s1.gpa = 3.7;
Structures are useful for modeling real-world data.
Examples:
Students
Employees
Products
Books
5. Accessing Structure Members
We use the dot operator (.) to access members.
Example:
printf("Name: %s\n", s1.name);
printf("ID: %d\n", s1.id);
printf("GPA: %.2f\n", s1.gpa);
The dot operator connects the structure variable with its fields.
6. Unions
A union is similar to a structure, but it stores data differently.
In a union:
All members share the same memory location.
Example:
union Data {
int i;
float f;
char str[20];
};
Here:
all share the same memory.
Only one member can store a value at a time.
Unions are used when memory efficiency is important.
7. Structures vs Unions
| Feature |
Structure |
Union |
| Memory |
Separate memory for each member |
Shared memory |
| Usage |
Store multiple values |
Store one value at a time |
| Size |
Sum of member sizes |
Size of largest member |
Structures are far more commonly used.
8. Advanced Data Structures (Preview)
As programs grow, we need more powerful structures.
Examples include:
Linked Lists
Stacks
Queues
Trees
Graphs
These structures are built using:
structures
pointers
dynamic memory
You will learn these later in your programming journey.
9. Choosing the Right Data Structure
Good programmers choose structures carefully.
You should ask:
How is the data organized?
How often will we search?
How often will we insert or delete items?
How much memory is available?
Different problems require different solutions.
10. Example Program Using Structures
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int id;
float gpa;
};
int main() {
struct Student s1;
strcpy(s1.name, "Alice");
s1.id = 1001;
s1.gpa = 3.8;
printf("Name: %s\n", s1.name);
printf("ID: %d\n", s1.id);
printf("GPA: %.2f\n", s1.gpa);
return 0;
}
This program stores information about a student using a structure.
11. Practice Exercises
Create a structure called Book with fields:
Create a structure called Employee with fields:
Write a program that stores and prints information for one student.
Final Thoughts
In this lesson you learned:
What data structures are
Basic vs derived data types
How structures organize related data
How unions share memory
Why choosing the right structure matters
Data structures are the backbone of efficient programming.
As you continue learning C, you will see how pointers, arrays, and structures combine to build powerful systems.
The more you practice using these structures, the more natural they will become.