Skip to main content

Command Palette

Search for a command to run...

Arrays – Storing Data in Blocks of Memory

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! 🚀

So far, we’ve been working with single variables:

int age = 20;
int score = 85;

That works fine when you only need one value.

But what if you need to store:

  • 5 test scores?

  • 10 student IDs?

  • 100 numbers?

Creating 100 separate variables would be messy.

This is where arrays come in.

1. What Is an Array?

An array is:

A collection of values of the same type stored next to each other in memory.

The important parts are:

  • Same data type

  • Stored in consecutive memory locations

  • Accessed using an index

2. Declaring an Array

Basic syntax:

data_type array_name[size];

Example:

int scores[5];

This creates:

  • An array named scores

  • That can hold 5 integers

  • Stored in continuous memory

Right now, the array is created but not initialized.

3. Initializing an Array

You can assign values when declaring:

int scores[5] = {85, 90, 78, 92, 88};

Now the array contains:

scores[0] = 85
scores[1] = 90
scores[2] = 78
scores[3] = 92
scores[4] = 88

4. Indexing (Very Important)

Array indexing starts at 0, not 1.

This is extremely important.

If the array has 5 elements:

int scores[5];

Valid indices are:

0, 1, 2, 3, 4

The last index is always:

size - 1

So for scores[5], the last index is 4.

5. Accessing Array Elements

You access elements using square brackets:

scores[0]
scores[1]

Example:

#include <stdio.h>

int main() {
    int scores[3] = {10, 20, 30};

    printf("%d\n", scores[0]);
    printf("%d\n", scores[1]);
    printf("%d\n", scores[2]);

    return 0;
}

Output:

10
20
30

6. Modifying Array Elements

You can change values using indexing:

scores[1] = 50;

Now the array becomes:

10, 50, 30

Arrays are mutable (meaning their elements can change.)

7. Memory Layout (Why Arrays Matter)

Arrays are stored in contiguous memory.

If this is memory:

Address:   100   104   108   112   116
Value:      85    90    78    92    88

Each integer takes 4 bytes (on most systems).

So the addresses increase by 4.

This layout is important because:

  • It makes arrays efficient.

  • It allows pointer arithmetic later (we’ll get there).

8. Looping Through an Array

Arrays become powerful when combined with loops.

Instead of printing each element manually:

printf("%d\n", scores[0]);
printf("%d\n", scores[1]);
printf("%d\n", scores[2]);

We use a loop:

#include <stdio.h>

int main() {
    int scores[3] = {10, 20, 30};

    for (int i = 0; i < 3; i++) {
        printf("%d\n", scores[i]);
    }

    return 0;
}

This prints all elements automatically.

9. Common Beginner Mistakes

❌ 1. Going Out of Bounds

scores[3] = 100;

If array size is 3, valid indices are 0–2.

Accessing scores[3] is undefined behavior.

C will not protect you.

It may:

  • Crash

  • Corrupt memory

  • Appear to work (but cause problems later)

Always stay within bounds.

❌ 2. Confusing Size With Last Index

If:

int a[5];

The last valid index is 4, not 5.

❌ 3. Forgetting Array Size in Loops

Always match your loop condition to the array size:

for (int i = 0; i < 5; i++)

Not:

i <= 5

10. Why Arrays Are Important

Arrays allow us to:

  • Store collections of data

  • Process bulk information

  • Prepare for strings

  • Understand memory structure

  • Work with pointers (next lesson)

Arrays are the foundation of more advanced data structures.

11. Practice Exercises

  1. Create an array of 5 integers and print them using a loop.

  2. Create an array of 4 numbers and calculate their sum.

  3. Ask the user to input 3 numbers and store them in an array.

  4. Find the largest number in an array.

Final Thoughts

Arrays teach you something important about C:

C gives you power.

But it does not protect you from mistakes.

You must:

  • Manage memory carefully

  • Respect array boundaries

  • Think about how data is stored

In the next lesson, we’ll go deeper into memory by learning about pointers.

And that’s where C starts becoming truly powerful.

C Programming

Part 22 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

Function Prototypes and Variable Scope – How C Organizes Code

In the previous lesson, you learned how to: Define functions Call functions Use parameters Return values Now we go one level deeper. Today we will understand: Why function prototypes exist How

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.