Lesson 12: One-dimensional arrays
So far each variable held a single value. An array is a row of cells of the same type under one name: int grades[5]; reserves five consecutive int cells in memory. Each cell is accessed by an index that starts at 0 — the first cell is grades[0] and the last is grades[4]. With a for loop and a size c
An array is like a row of mailboxes at a building entrance: one name for the whole row, and a number for each box. Except in C you start counting from 0 — and if you try to open a box that doesn't exist, nobody stops you, you just open the wall.
- array
- A row of same-type cells, consecutive in memory, under one name. E.g. int grades[5];.
- index
- The number of a cell in the array. Counting starts at 0: first is a[0], last is a[SIZE-1].
- initializer list
- Curly braces that fill the array at declaration: {90, 85, 70, 88, 95}.
- size constant
- One name for the array size, e.g. #define SIZE 5, so the loop and the array always agree.
- out of bounds
- Accessing an index that doesn't exist (like a[7] in an array of 5). C doesn't check — it's undefined behavior.