Lesson 66 min read

C Arrays

A row of lockers in a hallway β€” each numbered, each the same size

A Row of Lockers

Imagine a hallway with a row of lockers. Each locker is the same size, each has a number painted on the door, and they sit side by side in memory. That's a C array.

When you declare an array, you're telling the compiler: "Reserve me 5 lockers in a row, all holding integers." The compiler finds a contiguous chunk of memory, slaps numbers 0 through 4 on the doors, and hands you the key to locker 0.

Declaring and Initializing

There are a few ways to create an array in C:

  • int scores[5]; β€” 5 lockers, contents unknown (garbage values!)
  • int scores[5] = {90, 85, 77, 92, 88}; β€” 5 lockers, each filled
  • int scores[] = {90, 85, 77, 92, 88}; β€” compiler counts for you

The index starts at 0, not 1. So scores[0] is the first element and scores[4] is the last in a 5-element array.

Declaring and Accessing Arrays

#include <stdio.h>
int main() {
int scores[5] = {90, 85, 77, 92, 88};
// Access individual elements
printf("First score: %d\n", scores[0]);
printf("Third score: %d\n", scores[2]);
printf("Last score: %d\n", scores[4]);
// Modify an element
scores[2] = 80;
printf("Updated third: %d\n", scores[2]);
return 0;
}
Output
First score:  90
Third score:  77
Last score:   88
Updated third: 80

Looping Through an Array

Arrays and loops are best friends. You almost never access each element by hand β€” you let a for loop walk down the hallway, opening each locker in turn.

One critical thing: C does not store the length of an array anywhere. You have to track it yourself. A common trick is sizeof(arr) / sizeof(arr[0]), but this only works when the array is in the same scope β€” it breaks the moment you pass the array to a function.

Filling and Printing with a Loop

#include <stdio.h>
int main() {
int nums[5];
// Fill with squares: 0, 1, 4, 9, 16
for (int i = 0; i < 5; i++) {
nums[i] = i * i;
}
// Print them out
int length = sizeof(nums) / sizeof(nums[0]);
printf("Array length: %d\n", length);
for (int i = 0; i < length; i++) {
printf("nums[%d] = %d\n", i, nums[i]);
}
return 0;
}
Output
Array length: 5
nums[0] = 0
nums[1] = 1
nums[2] = 4
nums[3] = 9
nums[4] = 16
Note: C does NOT check array bounds. If you write scores[10] on a 5-element array, the compiler won't stop you. You'll silently read or corrupt whatever memory happens to be there. This is one of the most common sources of bugs and security vulnerabilities in C programs.

Multi-Dimensional Arrays

Need a grid? A 2D array is just an array of arrays β€” like a spreadsheet with rows and columns. You declare it with two sets of brackets: int grid[3][4] gives you 3 rows and 4 columns.

In memory, it's still one contiguous block. Row 0 comes first, then row 1, then row 2 β€” all laid out flat. C is row-major.

2D Array β€” A Tic-Tac-Toe Grid

#include <stdio.h>
int main() {
// 3x3 grid: 1=X, 2=O, 0=empty
int board[3][3] = {
{1, 0, 2},
{0, 1, 0},
{2, 0, 1}
};
// Print the board
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (board[row][col] == 1)
printf(" X ");
else if (board[row][col] == 2)
printf(" O ");
else
printf(" . ");
}
printf("\n");
}
return 0;
}
Output
 X  .  O 
 .  X  . 
 O  .  X 

Passing Arrays to Functions

When you pass an array to a function, you're not passing a copy. You're passing a pointer to the first element. This means the function can modify the original array β€” there's no protection.

Because the size information is lost, you must pass the length as a separate argument. This is a classic C pattern you'll see everywhere.

Array as Function Parameter

#include <stdio.h>
void doubleAll(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2; // modifies original!
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int nums[] = {1, 2, 3, 4, 5};
int len = sizeof(nums) / sizeof(nums[0]);
printf("Before: ");
printArray(nums, len);
doubleAll(nums, len);
printf("After: ");
printArray(nums, len);
return 0;
}
Output
Before: 1 2 3 4 5 
After:  2 4 6 8 10 
Challenge

Quick check

What is the index of the LAST element in int data[8];?
← LoopsStrings & Characters β†’