Skip to main content

Command Palette

Search for a command to run...

Introduction to Data Structures in C

Published
4 min read
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

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.

C Programming

Part 18 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 next

Strings in C – Arrays of Characters

In the previous lessons, you learned about: Arrays Pointers How arrays and pointers are related Now we will apply that knowledge to something very common in programming: strings. In C, strings ar

More from this blog

D

Dr. Ehoneah Obed

75 posts

Software engineer writing about systems: in code, in learning, in life. I reverse-engineer complex problems into frameworks. Pharmacist → SWE → Founder.