Pointers & Arrays
Few topics in C trip up newcomers as reliably as the pointer/array relationship. Let's untangle it.
Arrays Decay to Pointers
Drop an array name into most expressions and it silently converts (the C standard calls this "decay") into a pointer to element zero:
int arr[] = {10, 20, 30};int *p = arr; // no & needed - arr decays to &arr[0]There are three exceptions where the array stays an array:
- With
sizeof—sizeof(arr)reports the full array's bytes, not a pointer's. - With
&—&arryields a pointer to the whole array (different type, same address as&arr[0]). - As a string literal initialiser —
char s[] = "hello"copies the bytes intos.
Multidimensional Arrays
A 2D array in C is one flat slab of memory laid out row by row:
int grid[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};In RAM that becomes a flat sequence: 1 2 3 4 5 6 7 8 9 10 11 12.
An int grid[3][4] is not an array of pointers. It's a single contiguous run of 12 integers. The compiler turns grid[i][j] into *(*(grid + i) + j), which boils down to an offset of i * 4 + j ints from the base.
Arrays of Pointers vs Pointer to Array
These two declarations sit one parenthesis pair apart and mean entirely different things:
int *ptrs[5]; // array of 5 pointers to intint (*row)[5]; // pointer to an array of 5 intsThe first form is the bread-and-butter way to hold a list of strings:
const char *names[] = {"Alice", "Bob", "Charlie"};Each slot stores a pointer at the first byte of a literal somewhere in read-only memory.
Passing Arrays to Functions
Because arrays decay, these two signatures compile to exactly the same thing:
void process(int arr[], int len);void process(int *arr, int len);Either way, arr is a pointer at runtime, and the function has no idea how many elements sit beyond it. Pass the length explicitly. Get that length wrong and you've just written a buffer overflow.
You can watch the decay happen. Guess both numbers before you run this.
Writing the parameter as int arr[10] changes nothing: the 10 is decoration. GCC even has a warning on by default for this exact mistake (-Wsizeof-array-argument), because sizeof on an array parameter gives the size of a pointer, 8 bytes on x86-64, and never the 40 you were hoping for.
Dynamic 2D Arrays
For a dynamic 2D array, the typical recipe is: malloc a row of pointers, then malloc each row's payload.
int **grid = malloc(rows * sizeof(int *));if (grid == NULL) return -1;for (int i = 0; i < rows; i++) { grid[i] = malloc(cols * sizeof(int)); if (grid[i] == NULL) { // undo the rows we already got while (i-- > 0) free(grid[i]); free(grid); return -1; }}
// Use grid[i][j] normally
for (int i = 0; i < rows; i++) free(grid[i]);free(grid);You get the familiar grid[i][j] syntax, but now the rows live on the heap and you can resize them. The price: rows aren't contiguous, so cache locality takes a hit compared to a flat int grid[3][4].
Here's the difference for three rows of four ints. The flat array is one 48-byte block, and grid[1][2] is just the int at offset (1 * 4 + 2) * 4 from the start. The pointer version is four separate allocations, and grid[1][2] costs an extra load to fetch the row pointer first:
Nothing guarantees the three rows sit near each other, or in order. Each malloc puts its block wherever the allocator likes.