Nearby lessons
52 of 124C - Array Introduction
An array stores many values of the same type under one name. Learn why arrays exist, how they are laid out in memory, and the zero-based indexing rule that trips up every beginner.
The Problem Arrays Solve
Suppose you need the marks of five students. Without arrays you would declare five separate variables — and the code gets worse with every student you add:
The Same Job with an Array
One name, one loop, and the code no longer grows with the number of students:
What an Array Really Is
An array is a block of memory holding a fixed number of elements, all of the same data type, stored one immediately after another.
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Value | 85 | 90 | 78 | 92 | 88 |
| Address | 1000 | 1004 | 1008 | 1012 | 1016 |
Each int takes 4 bytes, so the addresses step by 4. Because the elements are evenly spaced, C can jump straight to any element with one multiplication — which is why array access is so fast.
Why Indexing Starts at 0
The index is not a position — it is an offset from the start of the block. The first element is 0 boxes away from the beginning, so its index is 0.
The Off-By-One Trap
An array of size 5 has valid indexes 0 to 4. There is no nums[5], and C will not stop you from using it:
C Does Not Check Bounds
nums[5] or nums[1000] compiles without a single warning. It reads whatever bytes happen to be there — another variable, or memory that is not yours. Writing out of bounds silently corrupts your program and may crash it much later, far from the real bug.Languages like Java and Python throw an exception here. C hands you the responsibility, so always loop with i < size, never i <= size.
Advantages and Limitations
| Advantages | Limitations |
|---|---|
| One name for many values | Fixed size — cannot grow at runtime |
| Instant access by index | All elements must be the same type |
| Works naturally with loops | No bounds checking |
| Cache-friendly and fast | Inserting or deleting in the middle means shifting elements |
| Easy to pass to functions | Cannot be assigned or compared with = or == |
malloc and realloc. That is a later topic — master fixed arrays first.- An array is a fixed-size collection of same-type values.
- Indexing starts at 0, so the last index is size - 1.
- Elements are stored side by side in contiguous memory.
- The size must be known at compile time (in classic C).
- C never checks whether your index is in range.