Arrays
The first and most fundamental data structure. Boxes lined up in memory — simple to understand, powerful in practice, used absolutely everywhere.
Quick answer
An array stores elements of one type in consecutive memory slots, so element i is found instantly at base address + i × element size. Reading by index is O(1); inserting or deleting in the middle means shifting elements, which is O(n).
- Indices run from 0 to n−1; index n is out of bounds.
- Contiguous memory is what makes indexing O(1).
- Insert or delete at position k shifts everything after it.
#include <stdio.h>
int main(void) {
int a[6] = {10, 20, 30, 40, 50};
int n = 5;
printf("a[2] = %d\n", a[2]);
// insert 25 at index 2: shift elements right
for (int i = n; i > 2; i--) a[i] = a[i - 1];
a[2] = 25;
n++;
for (int i = 0; i < n; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}Output
a[2] = 30 10 20 25 30 40 50
Everything in data structures starts here. Linked lists, stacks, queues, heaps, graphs — they all either use arrays internally or are compared against arrays. If you understand arrays deeply, the rest of DSA becomes much easier to learn.
In this lesson we build arrays from scratch in C, understand exactly how they live in your computer's memory, and implement every core operation with full code and complexity analysis.
What is an Array?
An array is a collection of items stored at contiguous (back-to-back) memory locations, where every item is of the same data type.
The word contiguous is the key. It means the items do not scatter randomly in RAM — they sit in a neat, unbroken row. This single property is what gives arrays their superpower: accessing any element instantly in O(1).
🪑 Think of a row of seats in a cinema
Imagine Row A in a cinema hall. It has seats A1, A2, A3, A4, A5 — all in a straight line, numbered from left to right. Every seat exists, every seat has a number, and they are all together.
address of arr[i] = base address + (i × size of one element)This is why accessing arr[999] is just as fast as accessing arr[0]. No searching needed — pure arithmetic.
How Arrays Sit in Memory
Let us go one level deeper and see exactly what happens in RAM when you create an array in C. This is the part most tutorials skip — and it is the part that makes everything else click.
Say you declare this:
An int takes 4 bytes in C. So 5 integers need 5 × 4 = 20 bytes of contiguous RAM. Here is what that looks like:
To find marks[3], the computer does:
address = 0x2000 + (3 × 4)
address = 0x2000 + 12
address = 0x200C → value is 60 ✓
Declaring and Accessing Arrays in C
There are three ways to create an array in C. Each one is useful in different situations.
arr[5] goes out of bounds — you are reading memory that does not belong to you. C will not warn you. It will silently read garbage data or crash your program. Always remember: last valid index = size − 1.Traversal — Visiting Every Element
Traversal means visiting each element of the array exactly once — from index 0 to index n−1. It is the most basic operation and the foundation of everything else.
We visit n elements so time is O(n). We use no extra memory — just one loop variable i — so space is O(1).
Insertion — Adding an Element
Here is an important truth about arrays: their size is fixed when you create them. You declare int arr[10] and you get exactly 10 slots — no more, no less. So "insertion" in an array means placing a value into an existing empty slot, or shifting elements to make room.
Case 1 — Insert at the end
If there is space at the end, just put it there. No shifting needed.
Case 2 — Insert at a specific position
This is where arrays show their weakness. To insert at position 2, every element from position 2 onwards must shift one step to the right to make room. The more elements, the more shifting.
Deletion — Removing an Element
Deletion is the mirror image of insertion. To remove an element at a position, you shift everything after it one step to the left to fill the gap. The array size in memory stays the same — you just track one fewer filled element.
Searching — Finding an Element
To find a value in an unsorted array, the only option is to check each element one by one until you find it or reach the end. This is called linear search.
2D Arrays — Rows and Columns
A 2D array is an array of arrays — like a table or a grid. Think of a spreadsheet: rows going across, columns going down. Every cell has two coordinates — a row number and a column number.
Classic Array Problems — With Full Solutions
These four problems appear constantly in interviews and in real code. Understand every line — do not just copy.
Array Strengths and Weaknesses
No data structure is perfect. Understanding where arrays shine and where they struggle is exactly the kind of thinking interviewers test for.
| Operation | Best Case | Worst Case | Notes |
|---|---|---|---|
| Access by index | O(1) | O(1) | Always instant — best property of arrays |
| Search (unsorted) | O(1) | O(n) | Best: element at index 0. Worst: not found |
| Insert at end | O(1) | O(1) | Just place in the next slot |
| Insert at position | O(1) | O(n) | Worst: insert at index 0 shifts everything |
| Delete at position | O(1) | O(n) | Worst: delete index 0 shifts everything |
| Traversal | O(n) | O(n) | Must visit all n elements |
You Are Ready for Lesson 4
You now understand arrays completely — from how they sit in RAM to every core operation with full complexity analysis. This knowledge will never become irrelevant. Arrays are inside almost every program ever written.
In Lesson 4 we cover Strings — which are, at their core, just arrays of characters. You already know how they work in memory. Now we learn how to search, reverse, compare, and manipulate text in C.
🎯 Key Takeaways
- ✓An array stores elements at contiguous (back-to-back) memory locations — all of the same type
- ✓Accessing any element by index is O(1) because the address is calculated with simple math: base + (index × size)
- ✓Arrays are zero-indexed — valid indices are 0 to n−1. Accessing index n is an out-of-bounds error
- ✓Insertion and deletion at a position require shifting elements — worst case O(n)
- ✓Linear search checks every element one by one — O(n) worst case
- ✓2D arrays are tables: accessed as matrix[row][column], traversal is O(rows × cols)
- ✓Arrays are fast to access but slow to insert/delete. This weakness motivates linked lists in Lesson 9
Quick check
Why is reading a[i] O(1)?
PUT IT INTO PRACTICE
Find a pair with a target sum
Given [2, 7, 11, 15] and target 9, print the two zero-based indices whose values add to the target.
Run or work through the task, then mark it complete when you have checked your result.
HELP US IMPROVE
How was this lesson?
Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.
Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.
