Skip to main content

Command Palette

Search for a command to run...

Arrays and Pointers – Understanding Their Relationship

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

In the last lesson, we learned:

  • What pointers are

  • How & gives an address

  • How * dereferences a pointer

Now we connect that knowledge to arrays.

This is the moment many students say:

“Ohhhh… so that’s what’s really happening.”

By the end of this lesson, you will understand:

  • Why array names behave like pointers

  • Why a[i] and *(a + i) mean the same thing

  • What pointer arithmetic is

  • The difference between arrays and pointers

1. The Most Important Idea

When you declare an array:

int a[5] = {10, 20, 30, 40, 50};

The name a represents:

The address of the first element.

So:

a == &a[0]

That is extremely important.

The array name behaves like a pointer to its first element.

2. Visualizing Memory

Suppose memory looks like this:

Address   Value
1000      10
1004      20
1008      30
1012      40
1016      50

If a stores address 1000, then:

a      → 1000
a + 1  → 1004
a + 2  → 1008

Each time you add 1, it moves by the size of an int.

That is pointer arithmetic.

3. Pointer Arithmetic

Let’s look at this code:

#include <stdio.h>

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

    printf("%d\n", *a);
    printf("%d\n", *(a + 1));
    printf("%d\n", *(a + 2));

    return 0;
}

Output:

10
20
30

Why?

Because:

*a       == a[0]
*(a+1)   == a[1]
*(a+2)   == a[2]

So this means:

a[i] == *(a + i)

They are equivalent.

4. Why This Works

When you write:

a[i]

C internally translates it to:

*(a + i)

So indexing is just pointer arithmetic combined with dereferencing.

This is not magic.

It is simple math with memory addresses.

5. Using a Pointer to Traverse an Array

You can also use a pointer variable:

#include <stdio.h>

int main() {
    int a[3] = {10, 20, 30};
    int *p = a;

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

    return 0;
}

Here:

  • p stores the address of a[0]

  • p + 1 moves to the next element

  • *(p + i) retrieves the value

6. Are Arrays and Pointers the Same?

They behave similarly.

But they are NOT the same.

Important differences:

1. Arrays have fixed size

int a[5];

You cannot change the size of a.

2. Array name cannot be reassigned

This is illegal:

a = some_other_address;   // Error

Array names are constant addresses.

3. Pointers can be reassigned

int x = 10;
int y = 20;

int *p = &x;
p = &y;   // Allowed

Pointers are flexible.

Arrays are fixed.

7. Why This Matters

Understanding arrays and pointers helps you:

  • Work with strings

  • Pass arrays to functions

  • Manage memory

  • Understand how C really works

If you skip this understanding, advanced topics will feel confusing.

8. Common Mistakes

❌ Confusing pointer arithmetic

When you write:

p + 1

It does NOT move by 1 byte.

It moves by:

sizeof(data_type)

If p is int*, it moves 4 bytes (usually).

❌ Forgetting array bounds

Even with pointer arithmetic, you must stay inside the array.

C will not protect you.

9. Practice Exercises

  1. Create an array of 4 integers and print them using:

    • indexing (a[i])

    • pointer arithmetic (*(a + i))

  2. Assign a pointer to an array and use it to modify elements.

  3. Draw memory layout for:

    int a[3] = {5, 6, 7};
    
  4. Explain why a[2] equals *(a + 2).

Final Thoughts

Today you learned something powerful:

  • Array names are addresses.

  • Indexing is pointer arithmetic.

  • a[i] is the same as *(a + i).

  • Arrays and pointers are related but not identical.

This understanding makes C much clearer.

In the next lesson, we will move to strings — which are just arrays of characters.

And everything you learned today will make that lesson easier.

C Programming

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

Pointers – Understanding Memory Addresses

If arrays taught you that data is stored next to each other in memory,pointers will teach you how to access memory directly. This is where C becomes powerful and a little scary. But don’t worry. We’ll

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.