← C ProgrammingC PROGRAMMING 1.3

Strings and Arrays

Arrays, C strings, and hand-implementing the standard-library classics from scratch.

Arrays and C strings, and then at the end, actually hand-writing the standard-library functions I'd been calling for granted the whole time — that last chapter changed how I read every string function since.

The six chapters

  • A — Array Fundamentals. Declaration, indexing, the fact that the size is fixed forever once declared, zero bounds checking, and decay the moment an array is passed to a function.
  • B — Multidimensional Arrays. Row-major layout and how indexing a 2D array actually resolves to one flat block of memory underneath.
  • C — C Strings. A string is just a char array with an agreed-upon ending — the null terminator — and most string bugs are really just forgetting that agreement exists.
  • D — String Library Functions. strlen/strcpy/strcmp and friends, plus the sharp edges that make half of them genuinely dangerous to use carelessly.
  • E — Arrays vs Pointers. Where the two are close enough to be interchangeable, and the specific places — sizeof, reassignment — where they very much are not.
  • F — Implement the Classics. Writing my_strlen, my_strcpy/my_strncpy, my_strcmp, my_memcpy, my_memmove, and my_atoi by hand instead of just calling them.

Where this lands in driver work

No bounds checking on arrays is exactly the same deal as touching hardware memory directly — nothing stops an off-by-one from writing past a buffer into whatever happens to sit next to it. Parsing a byte stream off a UART or a packet off a bus is, mechanically, the same array-walking discipline as chapter C and D, just with hardware instead of a text string on the other end.

Notes from readers

Comments — via GitHub