By the end of this lesson, cadets will be able to:
Complete before class.
| Source | Sections | Why |
|---|---|---|
| Beej-C | Chapter 6, sections 6.1 through 6.5 | Declaration, initializers, bounds, multidimensional arrays |
{0} and designated [i]=v formssizeof(a)/sizeof(a[0]) length idiomAn array is a fixed-length row of values, all of the same type, stored back-to-back in memory and accessed by integer index starting at 0. Coming from Python, this will feel almost familiar but not quite: the size is fixed, the indices are bounded, and C does no checking. The reference material below covers what you will see in lecture and use in the walkthrough.
The Lesson 9 deck covers the array model, declaration and initialization, indexing, bounds, the length idiom, and common bugs. The slides are the spine of the in-class lecture; the array walkthrough is where you practice each concept hands-on.
In CS110 you used Python lists: scores = [88, 92, 75, 60, 95]. The list grew when you appended, shrank when you popped, knew its own length via len(scores), and raised an exception if you indexed past the end.
A C array is a different beast wearing similar syntax. It is a fixed-length block of memory carved out at declaration time. It does not know its own length, cannot grow or shrink, and accessing past the end produces undefined behavior rather than an exception.
| Behavior | Python list | C array |
|---|---|---|
| Size | Grows and shrinks at runtime | Fixed at declaration; cannot change |
| Element type | Mixed types allowed | All elements must be the same type |
| Knows its length | len(lst) works anywhere |
sizeof(a)/sizeof(a[0]), declaration scope only |
| Out of bounds | IndexError raised |
Undefined behavior; no error |
| Memory layout | Implementation-managed | Contiguous, predictable |
The trade is real. In return for the discipline you get speed, predictable memory layout, and the ability to reason about exactly where each element lives. The cost is that you carry the bounds-checking responsibility yourself.
An array declaration has three parts: the element type, the array name, and the size in square brackets.
Three things to remember:
#define'd constant).append.You can initialize an array at the point of declaration using an initializer list in braces.
Three variants are worth knowing:
Zero-fill the rest. If the initializer has fewer values than the array length, C zero-fills the remaining cells.
Inferred size. If you leave the brackets empty but provide an initializer, C counts the elements for you.
Designated initializers. You can set specific indices by name. After a designated entry, subsequent values continue from there.
Read or write a single element using square brackets with an integer index. Indices start at 0.
0 through N-1. The last valid index is always one less than the length.C performs no runtime bounds checking. Reading or writing an out-of-bounds index produces undefined behavior: the C standard says anything is allowed to happen. In practice you might see:
Out-of-bounds writes are typically worse than reads. A read returns a bad value but leaves memory intact; a write modifies memory you do not own, potentially corrupting other variables, the call stack, or runtime data. The walkthrough will let you watch this happen.
C arrays do not store their length. If you need to know it, you compute it from the total byte size divided by the size of one element.
Two style notes. First, prefer sizeof(a[0]) over sizeof(int); the first survives a type change without rewriting the expression. Second, this only works in the scope where the array is declared.
Almost every array bug in your future career is a variation of one of these four. Internalize them now.
| Bug | What it looks like |
|---|---|
| Off-by-one in a loop | Writing for (i = 0; i <= n; i++) instead of i < n. The last iteration reads or writes a[n], one past the end. |
| Using length where last index is wanted | Asking for "the last element" with a[length] instead of a[length - 1]. |
| Mismatched length parameter | Passing a hard-coded count to a function that does not match the actual length of the array you passed. |
| Reading uninitialized cells | Declaring int a[5]; at function scope, then reading a[0] before any write. The compiler does not warn; the value is whatever was on the stack. |
Three things to do in class. Use the checklist to track your progress.
Nine tasks across four sections. Type each C statement at the prompt. The terminal shows what happened; the right pane shows the array state with index labels and ghost cells for out-of-bounds accesses.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Lesson 9 reflection.
Eight questions. Some are quick recall; some are subtle gotchas. Answer each one and click Submit. The final card shows your score and a completion code. You need at least 70% (6 out of 8) for a passing code.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Lesson 9 reflection.