Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. c-programming
  3. arrays
9 min read

Arrays

Now that you know where data lives — the stack and the heap — we can start building structures inside that memory. The simplest one is the array: a fixed number of values of the same type, laid out back to back. You have seen arrays in passing already; this lesson treats them properly, because almost every challenge in this section is really about getting data into an array and walking back over it.

Static Arrays

A static array has its size written into the source code. You declare it with the element type, a name, and a count in square brackets:

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

That reserves room for five ints, side by side, and fills them in. You reach an element by its index in square brackets, and C counts from zero — so the first element is arr[0] and the last is arr[4]:

c
1printf("%d\n", arr[0]); // 10
2arr[4] = 99;            // change the last element
arr[0]10 arr[1]20 arr[2]30 arr[3]40 arr[4]50 arr[5]out ofbounds valid indices: 0 to 4 (length − 1)

Here is the single most important thing to know about C arrays: there is no bounds checking. Nothing stops you from writing arr[5], or arr[100], on a five-element array. The compiler will not complain and the program will not politely error — it will read or write whatever memory happens to sit past the end of the array. Sometimes that crashes; sometimes it silently corrupts other data; and sometimes it is exactly the foothold an attacker needs. Staying inside 0 to length − 1 is entirely your responsibility.

Off-by-one is the classic array bug
A loop that runs for (int i = 0; i <= 5; i++) over a five-element array touches arr[5], one step too far. Use i < length, not i <= length.

Arrays and Pointers

Arrays and pointers are deeply related. In most expressions, an array's name decays into a pointer to its first element — so arr is the same address as &arr[0], and indexing is really just the pointer arithmetic from the pointers lesson in disguise:

c
arr[i] == *(arr + i) // these are identical

But an array is not the same thing as a pointer, and one difference bites everyone eventually: sizeof. On the array itself, sizeof gives the size of the whole array; on a pointer, it gives the size of just the pointer. We can watch the difference in the debugger:

arrays.c
1#include <stdio.h>
2
3int main(void)
4{
5    int arr
gdb
$ gcc -g arrays.c -o arraysgef➤ set output-radix 10gef➤ break 8Breakpoint 1 at 0x116c: file arrays.c, line 8.gef➤ runBreakpoint 1, main () at arrays.c:8gef➤ p arr$1 = {10, 20, 30, 40, 50}gef➤ p arr[2]$2 = 30gef➤ p sizeof(arr)$3 = 20gef➤ p sizeof(p)$4 = 8gef➤ x/5dw arr0x7fffffffe1c0: 10      20      30      400x7fffffffe1d0: 50

sizeof(arr) is 20 — five ints of four bytes each — while sizeof(p) is 8, just the pointer. This matters because the moment you pass an array to a function, it decays to a pointer and that size information is lost. That is why C functions take an array and its length as separate arguments: the function has no other way to know how long the array is.

Dynamic Arrays

A static array's size is fixed when you compile. When you only learn the size at runtime, you build a dynamic array on the heap with malloc, exactly as in the memory lesson — and then you index it with [] just like a static one:

dynarr.c
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
$ gcc dynarr.c -o dynarr$ ./dynarr0 1 4 9 16

The indexing syntax is identical — arr[i] works the same whether arr points at stack or heap memory. The only differences are that you chose the size at runtime, and that you are responsible for the matching free. If you later need it bigger, realloc grows it.

Multi-Dimensional Arrays

An array can hold arrays, giving you a grid. int grid[2][3] is two rows of three columns, and you index it with two brackets, row first:

grid.c
1#include <stdio.h>
2
3int main(void)
4{
5    int grid

You might picture that as a 2-D table, but memory is one-dimensional, so C stores it row-major: the whole first row, then the whole second row, all contiguous.

As a table: 1 2 3 ← row 0 4 5 6 ← row 1 In memory (row-major): 1 2 3 4 5

The debugger confirms the layout — grid holds the six values, grid[1][2] is 6, and examining the raw memory shows all six in one unbroken line:

gdb
$ gcc -g grid.c -o gridgef➤ set output-radix 10gef➤ break 7Breakpoint 1 at 0x116b: file grid.c, line 7.gef➤ runBreakpoint 1, main () at grid.c:7gef➤ p grid$1 = {{1, 2, 3}, {4, 5, 6}}gef➤ p grid[1][2]$2 = 6gef➤ x/6dw grid0x7fffffffe1c0: 1       2       3       40x7fffffffe1d0: 5       6

Because the rows are contiguous, grid[1][0] sits immediately after grid[0][2] in memory — useful to remember when a challenge hands you a flat buffer that is really a grid. With arrays understood, we can look at the most common array of all: the string.

[
5
]
=
{
10
,
20
,
30
,
40
,
50
}
;
6 int *p = arr;
7
8 printf("%d\n", arr[0]);
9 return 0;
10}
5{
6 int n = 5;
7 int *arr = malloc(n * sizeof(int));
8
9 for (int i = 0; i < n; i++)
10 arr[i] = i * i;
11
12 for (int i = 0; i < n; i++)
13 printf("%d ", arr[i]);
14 printf("\n");
15
16 free(arr);
17 return 0;
18}
[
2
]
[
3
]
=
{
{
1
,
2
,
3
}
,
{
4
,
5
,
6
}
}
;
6
7 printf("%d\n", grid[1][2]);
8 return 0;
9}
6[0][0][0][1][0][2][1][0][1][1][1][2]