Arrays: straight to any index
A C array is one block of memory with the elements side by side, so the address of a[i] is the start plus i elements and reading any element is O(1); indexes start at 0, and C never checks the bounds.
a[0] or a[999999]: which is faster to get? Neither. This lesson shows why: a C array is one block of memory, one element after another, so where element i is can be worked out, not searched for.
One after another
int a[5] = {10, 20, 30, 40, 50}; is five ints side by side in memory. An int takes 4 bytes, so each next one is 4 further on.
stack
a[0]int10a[1]int20a[2]int30a[3]int40a[4]int50(The addresses are an example: the real ones may change every run, but "4 apart" doesn't.)
In int a[5], a[0] is at address 2000, and an int takes 4 bytes. Where is a[1]?
sizeof measures how many bytes something takes. What does this program print?
1#include <stdio.h>2 3int main(void) {4 int a[5] = {10, 20, 30, 40, 50};5 printf("%d\n", (int)sizeof(a));6 return 0;7}
sizeof(a) / sizeof(a[0]) is 5.Working out the address
The elements sit side by side and are all the same size, so you never have to search for element i. Its address is simply:
address of a[i] = address of a + i × the size of one element
One multiplication and one addition, whatever i is and however long the array: getting an element by index is O(1). It is also why indexes start at 0: the first element is 0 elements from the start.
We don't print the addresses themselves (they change between runs), only how far apart they are:
1#include <stdio.h>2 3int main(void) {4 int a[5] = {10, 20, 30, 40, 50};5 // how many bytes apart two addresses are6 printf("a[1] is %d bytes after a[0]\n", (int)((char *)&a[1] - (char *)&a[0]));7 printf("a[3] is %d bytes after a[0]\n", (int)((char *)&a[3] - (char *)&a[0]));8 printf("an int takes %d bytes\n", (int)sizeof(int));9 return 0;10}
a[1] is 4 bytes after a[0] a[3] is 12 bytes after a[0] an int takes 4 bytes
A double takes 8 bytes. How many bytes after &d[0] is &d[4]?
1#include <stdio.h>2 3int main(void) {4 double d[10];5 printf("%d\n", (int)((char *)&d[4] - (char *)&d[0]));6 return 0;7}
short s[10]; A short takes 2 bytes, and s starts at address 1000. What is the address of s[6]?
Pointers work it out the same way
In C, a[i] is really *(a + i): used as a pointer, an array's name is the address of its first element, and + i multiplies by the element's size for you.
1#include <stdio.h>2 3int main(void) {4 int a[5] = {10, 20, 30, 40, 50};5 int *p = a; // points at a[0]6 printf("%d %d\n", a[3], *(p + 3));7 p = p + 2; // two ints further on8 printf("%d\n", *p);9 return 0;10}
40 40 30
p points at a[1]. What does this program print?
1#include <stdio.h>2 3int main(void) {4 int a[5] = {10, 20, 30, 40, 50};5 int *p = a + 1;6 printf("%d %d\n", *p, p[2]);7 return 0;8}
*p is what p points at, a[1], which is 20. p[2] is *(p + 2): 2 further on from a[1], so a[3] = 40. Indexes count from wherever the pointer points.Fill in a number so the program prints 30.
1#include <stdio.h>2 3int main(void) {4 int a[5] = {10, 20, 30, 40, 50};5 printf("%d\n", *(a + ));6 return 0;7}
a[2] is *(a + 2). a + 2 moves on 2 ints; the compiler multiplies by the 4 bytes itself.Backwards
✓Using *(a + i), print the array backwards, from the last element to the first.
50 40 30 20 10
1#include <stdio.h>2 3int main(void) {4 int a[5] = {10, 20, 30, 40, 50};5 // i from 4 down to 0, print *(a + i)6 printf("\n");7 return 0;8}
2-D arrays: one row after another
int m[2][3] is a table of 2 rows and 3 columns. But memory has no rows or columns, just one long strip, so C lays the table out row after row: first the 3 values of row 0, then straight after them the 3 of row 1. This is called row-major order.
stack
m[0][0]int1m[0][1]int2m[0][2]int3m[1][0]int4m[1][1]int5m[1][2]int6So to reach m[i][j], skip i whole rows (3 values each), then j more:
address of m[i][j] = start + (i × number of columns + j) × element size
Just like a 1-D array, it is one calculation away: still O(1). Exams love this one. Watch out: you multiply by the number of columns.
1#include <stdio.h>2 3int main(void) {4 int m[2][3] = {{1, 2, 3}, {4, 5, 6}};5 printf("%d\n", (int)((char *)&m[1][0] - (char *)&m[0][0]));6 printf("%d\n", (int)((char *)&m[1][2] - (char *)&m[0][0]));7 return 0;8}
12 20
m[1][0] has 1 × 3 + 0 = 3 ints before it, so it is 12 bytes on; m[1][2] has 1 × 3 + 2 = 5 before it, so 20 bytes on.
int m[3][4] has 3 rows and 4 columns; an int takes 4 bytes. How many bytes after &m[0][0] is &m[2][1]?
1#include <stdio.h>2 3int main(void) {4 int m[3][4];5 printf("%d\n", (int)((char *)&m[2][1] - (char *)&m[0][0]));6 return 0;7}
int m[4][5] is stored row by row from address 1000, and an int takes 4 bytes. What is the address of m[3][2]?
Past the end: C won't stop you
int a[5] has a[0] to a[4]. Write a[5] and the address still works out, but that memory isn't the array's: reading it gets something else, and writing it breaks another variable. C itself doesn't check indexes.
stack
a[3]int40a[4]int50sumint1501#include <stdio.h>2 3int main(void) {4 int a[5] = {10, 20, 30, 40, 50};5 int sum = 0;6 for (int i = 0; i <= 5; i++) // one step too far7 sum += a[i];8 printf("%d\n", sum);9 return 0;10}
index 5 out of bounds for type 'int [5]'What happened: Stopped at line 7. This goes past the end of an array (or a string). Real C would read or overwrite whatever is next in memory.
Press Run and see what happens. Our C stops right there and says which element was out of bounds; real gcc doesn't stop and works out some unpredictable number. The usual mistake is i <= n; it should be i < n.
int a[5]; Which of these loops goes past the end?
a[5] is not part of the array: the valid indexes are 0 to 4. This loop also skips a[0].This loop goes one step too far. Change only the number in the green box so it adds exactly the 5 elements and prints 150.
1#include <stdio.h>2 3int main(void) {4 int a[5] = {10, 20, 30, 40, 50};5 int sum = 0;6 for (int i = 0; i <= ; i++)7 sum += a[i];8 printf("%d\n", sum);9 return 0;10}
150
i <= 4, which is the same as the usual i < 5.Add them up
✓Finish the loop to add up the 6 numbers in the array.
30
1#include <stdio.h>2 3int main(void) {4 int a[6] = {3, 8, 1, 9, 4, 5};5 int sum = 0;6 // from index 0 to 57 printf("%d\n", sum);8 return 0;9}
Summary
An array is one block of memory: indexing is O(1)
- address of a[i] = start + i × element size
- a[i] is *(a + i); indexes start at 0
- C doesn't check the bounds: loop with i < n
1int a[5];2a[i] // element i: its address in one step3*(a + i) // the same thing4for (i = 0; i < n; i++) // not i <= n