Skip to main content

Introduction to Data Structures in C

Updated 4 min readMarkdown
Part of seriesC Programming

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:

  • a name

  • an ID

  • a GPA

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:

  • i

  • f

  • str

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

  1. Create a structure called Book with fields:

    • title

    • author

    • price

  2. Create a structure called Employee with fields:

    • name

    • ID

    • salary

  3. 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.

About the author

  • Heya! 👋 I love helping people, and one of the best ways I do this is by sharing what I learn and build. I started as a pharmacist at a tertiary hospital in Northern Ghana, where I saw how much technology could improve healthcare. That sparked my interest in digital health. I taught myself to code, then joined the ALX Software Engineering program, which became a turning point. I went on to complete a Master of Health Informatics at the University of Toronto. Today I am a software and AI engineer and a health informatician, and I practise remotely as a Digital Health Pharmacist. I combine clinical experience with technical skills to build tools that solve real problems in health and beyond, and I write here about what I build and what I learn.

No comments yet. Be the first to comment.

C Programming

Part 23 of 40

From today, I will be starting lessons on C programming in my ALX Software Engineering class and I look forward to sharing with you everything I learn through this series.

Up nextWhat is Recursion? Understanding the Idea FirstIn the previous weeks, you learned how to control program flow using: Conditions (if, else) Loops (for, while) Functions Functions helped us break large problems into smaller pieces. Now we take

More from this blog