# Arrays – Storing Data in Blocks of Memory

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

```c
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:

```c
data_type array_name[size];
```

Example:

```c
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:

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

Now the array contains:

```plaintext
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:

```c
int scores[5];
```

Valid indices are:

```plaintext
0, 1, 2, 3, 4
```

The last index is always:

```plaintext
size - 1
```

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

## 5\. Accessing Array Elements

You access elements using square brackets:

```c
scores[0]
scores[1]
```

Example:

```c
#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:

```plaintext
10
20
30
```

## 6\. Modifying Array Elements

You can change values using indexing:

```c
scores[1] = 50;
```

Now the array becomes:

```plaintext
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:

```plaintext
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:

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

We use a loop:

```c
#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

```c
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:

```c
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:

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

Not:

```c
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.
