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.
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:
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]:
1printf("%d\n", arr[0]); // 10
2arr[4] = 99; // change the last elementHere 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.
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 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:
arr[i] == *(arr + i) // these are identicalBut 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:
1#include <stdio.h>
2
3int main(void)
4{
5 int arrgcc -g arrays.c -o arraysset output-radix 10break 8Breakpoint 1 at 0x116c: file arrays.c, line 8.runBreakpoint 1, main () at arrays.c:8p arr$1 = {10, 20, 30, 40, 50}p arr[2]$2 = 30p sizeof(arr)$3 = 20p sizeof(p)$4 = 8x/5dw arr0x7fffffffe1c0: 10 20 30 400x7fffffffe1d0: 50sizeof(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.
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:
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
gcc dynarr.c -o dynarr./dynarr0 1 4 9 16The 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.
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:
1#include <stdio.h>
2
3int main(void)
4{
5 int gridYou 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.
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:
gcc -g grid.c -o gridset output-radix 10break 7Breakpoint 1 at 0x116b: file grid.c, line 7.runBreakpoint 1, main () at grid.c:7p grid$1 = {{1, 2, 3}, {4, 5, 6}}p grid[1][2]$2 = 6x/6dw grid0x7fffffffe1c0: 1 2 3 40x7fffffffe1d0: 5 6Because 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.