Lecture 24: Strings
Overview
Burton Ma explains how C represents strings as character arrays terminated by a null character, then uses string.h functions to teach the relationship between arrays, pointers, and memory safety. Examples cover string length, copying, and concatenation, including why undersized buffers, missing terminators, and overlapping source and destination strings can cause undefined behavior.
Key takeaways
- Every C string passed to functions such as printf with %s, strlen, strcpy, or strcat must have a reachable null terminator.
- Array capacity includes the null terminator, while string length does not: a string of length n generally needs at least n + 1 elements.
- C does not check array bounds, so writing beyond a char array can overwrite adjacent fields, as shown by a terminator replacing an int with zero.
- A string literal and a writable char array initialized from that literal are distinct objects: the literal is read-only, while the array's copied characters can be changed.
- strcpy and strcat do not provide destination-capacity checks, and their behavior is undefined when buffers are too small or the relevant strings overlap.
- Pointer arithmetic makes string traversal explicit: advancing a char pointer by one moves to the next character, and subtracting two pointers into the same array gives their element distance.
Chapters
- A C string is a char array whose end is marked by a null terminator with numeric value zero.
- The character '0' is different from the null terminator: in ASCII, '0' has value 48.
- C's string functions in string.h mainly support char arrays; wider character types and internationalization require attention to encoding.
- Using printf with the %s conversion on an array without a null terminator can read past the array until memory happens to contain zero.
- In Ma's example, adjacent arrays make one unterminated array appear to print twice, but the output depends on memory layout.
- Writing a fifth character into a four-element struct array overwrites the following int, demonstrating a buffer overrun and C's lack of array bounds checking.
- An array can contain several null-terminated strings, such as abc, def, and ghi, accessed at offsets 0, 4, and 8.
- A string literal initializes an array with an automatically added null terminator, so an array holding a three-character string needs four elements.
- An embedded null terminator makes string length stop at the first null even though the initialized array contains more characters.
- String literals have static storage duration and are treated as read-only.
- A pointer initialized from "hello" refers to the literal itself; attempting to modify its characters can crash.
- Initializing a char array from "hello" copies the characters into writable array storage, so modifying that array does not modify the literal.
- strlen takes a pointer to const char and returns a size_t count without modifying the string.
- The function scans until the first null terminator, so abc followed by a null and def has length 3.
- The const-qualified input communicates that callers should not expect the function to change the characters.
- An indexed implementation increments a counter while str[i] is not the null terminator.
- A pointer implementation checks *str, increments the count, and advances str one character at a time.
- The compact *str++ form combines dereferencing with post-increment, so it tests the current character before advancing the pointer.
- strcpy copies source characters into a destination array and includes the source's null terminator.
- The destination must have enough capacity for every source character plus the terminating null; a five-character string needs six elements.
- strcpy returns the original destination pointer, allowing its result to be passed directly to another function.
- strcpy can copy from or into interior positions by passing pointers such as dest + 5 or a pointer to the '2' in 220.
- An indexed implementation copies non-null characters and then copies the null terminator; a pointer loop can advance source and destination together.
- If source and destination overlap, strcpy behavior is undefined; it also requires both pointers to refer to valid null-terminated strings.
- strcat appends its second argument to the end of its first argument and copies the terminating null.
- Appending 56789 to 01234 requires an 11-element destination array, including space for the final null terminator.
- Insufficient destination capacity or overlapping strings can produce undefined behavior; the assignment requires implementing concatenation onto the same string.
Transcript excerpt
0:00 Okay. Good afternoon, everybody. So, we're in the strings notebook now. And this is also the topic of the assignment that was posted last night. So, useful lecture to be attending. So, quickly remind you what a string is. So, in C, a string is just a car array where the end of the string is marked by a character called the null terminator. So, remember the null terminator, its numeric value is zero, but its character would be backslash zero. So, the language provides very few functions to support strings. There's the usual ones that you would expect. But if you look at if you compare C
0:47 support for strings to something like Python or Java, C's support for strings is very primitive. So, there aren't very many functions that you need to know about. If you want to use one of these functions, they're probably in string.h. Now, that particular header file uses most of the functions there involve arrays of car. Now, car is typically an 8-bit type on most modern computing architectures. So, 8 bits means you have 255 different characters, which isn't very many. 256, sorry, different characters, which is fine for a language like English. But for, say, the Asian many of the Asian languages which have thousands of characters in them,
1:35 these sorts of functions don't work well with those types of languages. So, if you're poking around the the documentation for the C language, you might come across wcar_t. So, that's a wide character type. So, a wide character type is either 16 or 32 bits. And so, that can support many, many more characters. We're only using the the English characters. So, we we stick with char. Um so, even though there are a lot of these functions have support for these
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Burton Ma.